php-general Digest 26 Jan 2008 08:44:19 -0000 Issue 5257

Topics (messages 268134 through 268147):

Re: Functions are driving me crazy....
        268134 by: Nathan Nobbe
        268136 by: Nathan Nobbe
        268137 by: Richard Lynch
        268139 by: Thijs Lensselink
        268140 by: Nathan Nobbe
        268141 by: Thijs Lensselink
        268144 by: Jim Lucas

Re: php-general Digest 25 Jan 2008 07:59:28 -0000 Issue 5255
        268135 by: Richard Lynch

Re: how dod you get to do multiple mysql queries concurrently?
        268138 by: Jochem Maas
        268143 by: Richard Lynch

Re: Exception thrown without a stack frame
        268142 by: Richard Lynch
        268147 by: Nathan Rixham

Binary to double without loosing precision?
        268145 by: Nathan Rixham

Re: how do you get to do multiple mysql queries concurrently?
        268146 by: Nathan Rixham

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
<?PHP
/// initialize the return variable
$authenticated = false;

       function authentication(){
               if(!isset($user) || !isset($pass)) { return false; }  /// <--
i would do it a bit nicer than this, but you get the idea

               if($user && $pass) { // Keep in mind, PASSWORD has meaning in
MySQL
                       // Do your string sanitizing here
                       // (e.g. - $user = mysql_real_escape_string($
_POST['user']);)
                       $loginQuery = "SELECT * FROM login WHERE
user='".$user."' AND
Userpass='".$pass."' LIMIT 0,1;";
                       $loginResult = mysql_query($loginQuery) or die("Wrong
data supplied
or database error"  .mysql_error());
                       while($row1 = mysql_fetch_array($loginResult)) {
                               $_SESSION['user'] = $row1['User'];
                               $_SESSION['loggedin'] = "YES";
                               $authenticated = "true";
                       }
               }
       }return $authenticated;
?>

-nathan

--- End Message ---
--- Begin Message ---
oh; i just noticed you dont have formal parameters for $user or $pass;
so,

function authentication($user, $pass) {
/// ... then you can remove the first line i put in there on the last post
:)
}

-nathan

--- End Message ---
--- Begin Message ---

On Fri, January 25, 2008 1:59 pm, Jason Pruim wrote:
> Hi everyone :)
>
> So partly to get an answer, and partly to boost my post rankings for
> the week I have a question.
>
> I am attempting to write an authentication function which would query
> a database, check the username/password and return true if it matches.
> If it doesn't match, then it shouldn't return anything and they are
> denied access.
>
> Here is the code for the function:
>
> <?PHP
>
>       function authentication(){

//initialize the variable so it has SOMETHING in it before you try to
return it at the end:
$authenticated = false;

>               if($user && $pass) { // Keep in mind, PASSWORD has meaning in 
> MySQL

You need to pass in $user and $pass as arguments at the function
authentication ($user, $pass)

>                       // Do your string sanitizing here
>                       // (e.g. - $user = 
> mysql_real_escape_string($_POST['user']);)
>                       $loginQuery = "SELECT * FROM login WHERE 
> user='".$user."' AND
> Userpass='".$pass."' LIMIT 0,1;";
>                       $loginResult = mysql_query($loginQuery) or die("Wrong 
> data supplied
> or database error"  .mysql_error());
>                       while($row1 = mysql_fetch_array($loginResult)) {
>                               $_SESSION['user'] = $row1['User'];
>                               $_SESSION['loggedin'] = "YES";
>                               $authenticated = "true";
>                       }
>               }
>       }return $authenticated;
> ?>
>
> and here is the code that I am using to call it:
>
> $authenticated = authentication($user, $pass);
>
> but when ever I try and run it I get the following errors in my log
> file, and the page doesn't load the info in the database.
>
> Help me please!
>
>
>
> My error log shows this:
>
> [Fri Jan 25 14:55:14 2008] [error] PHP Notice:  Undefined variable:
> authenticated in /Volumes/RAIDer/webserver/includes/oldbinc/function/
> authentication.func.php on line 16
> [Fri Jan 25 14:55:14 2008] [error] PHP Notice:  Undefined variable:
> user in /Volumes/RAIDer/webserver/includes/oldbinc/function/
> authentication.func.php on line 5
> [Fri Jan 25 14:55:14 2008] [error] PHP Notice:  Undefined variable:
> user in /Volumes/RAIDer/webserver/includes/oldbinc/function/
> authentication.func.php on line 5
>
> --
>
> Jason Pruim
> Raoset Inc.
> Technology Manager
> MQC Specialist
> 3251 132nd ave
> Holland, MI, 49424
> www.raoset.com
> [EMAIL PROTECTED]
>
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
Jason Pruim wrote:
Hi everyone :)

So partly to get an answer, and partly to boost my post rankings for the week I have a question.

I am attempting to write an authentication function which would query a database, check the username/password and return true if it matches. If it doesn't match, then it shouldn't return anything and they are denied access.

Here is the code for the function:

<?PHP
function authentication(){ if($user && $pass) { // Keep in mind, PASSWORD has meaning in MySQL
            // Do your string sanitizing here
            // (e.g. - $user = mysql_real_escape_string($_POST['user']);)
$loginQuery = "SELECT * FROM login WHERE user='".$user."' AND Userpass='".$pass."' LIMIT 0,1;"; $loginResult = mysql_query($loginQuery) or die("Wrong data supplied or database error" .mysql_error());
            while($row1 = mysql_fetch_array($loginResult)) {
                $_SESSION['user'] = $row1['User'];
                $_SESSION['loggedin'] = "YES";
                $authenticated = "true";
            }
        }
    }return $authenticated;
?>
Change:

}return $authenticated;

to

   return $authenticated;
}

else it will never return the value from the function. But it will always give you undefined variable notice
--- End Message ---
--- Begin Message ---
On Jan 25, 2008 3:35 PM, Thijs Lensselink <[EMAIL PROTECTED]> wrote:

> Change:
>
> }return $authenticated;
>
> to
>
>    return $authenticated;
> }
>
> else it will never return the value from the function. But it will
> always give you undefined variable notice


nice catch ;)

-nathan

--- End Message ---
--- Begin Message ---
Nathan Nobbe wrote:
On Jan 25, 2008 3:35 PM, Thijs Lensselink <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Change:

    }return $authenticated;

    to

       return $authenticated;
    }

    else it will never return the value from the function. But it will
    always give you undefined variable notice


nice catch ;)

-nathan

and that on a friday evening :)

--- End Message ---
--- Begin Message ---
Jason Pruim wrote:
Hi everyone :)

So partly to get an answer, and partly to boost my post rankings for the week I have a question.

I am attempting to write an authentication function which would query a database, check the username/password and return true if it matches. If it doesn't match, then it shouldn't return anything and they are denied access.

Here is the code for the function:

<?PHP
function authentication(){
        if($user && $pass) { // Keep in mind, PASSWORD has meaning in MySQL
            // Do your string sanitizing here
            // (e.g. - $user = mysql_real_escape_string($_POST['user']);)
$loginQuery = "SELECT * FROM login WHERE user='".$user."' AND Userpass='".$pass."' LIMIT 0,1;"; $loginResult = mysql_query($loginQuery) or die("Wrong data supplied or database error" .mysql_error());
            while($row1 = mysql_fetch_array($loginResult)) {
                $_SESSION['user'] = $row1['User'];
                $_SESSION['loggedin'] = "YES";
                $authenticated = "true";
            }
        }
    }return $authenticated;
?>

and here is the code that I am using to call it:

$authenticated = authentication($user, $pass);

but when ever I try and run it I get the following errors in my log file, and the page doesn't load the info in the database.

Help me please!



My error log shows this:

[Fri Jan 25 14:55:14 2008] [error] PHP Notice: Undefined variable: authenticated in /Volumes/RAIDer/webserver/includes/oldbinc/function/authentication.func.php on line 16 [Fri Jan 25 14:55:14 2008] [error] PHP Notice: Undefined variable: user in /Volumes/RAIDer/webserver/includes/oldbinc/function/authentication.func.php on line 5 [Fri Jan 25 14:55:14 2008] [error] PHP Notice: Undefined variable: user in /Volumes/RAIDer/webserver/includes/oldbinc/function/authentication.func.php on line 5

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
[EMAIL PROTECTED]




<?PHP

function authentication(){
  // Per your example below, you need to call the escaping before your
  // if () statement

  // Do something like an if ( isset($_POST['user']) ) {..}
  $user = mysql_real_escape_string(@$_POST['user']);

  // Do something like an if ( isset($_POST['pass']) ) {..}
  $pass = mysql_real_escape_string(@$_POST['pass']);

  // Keep in mind, PASSWORD has meaning in MySQL
  if($user && $pass) {

    // Do your string sanitizing here
    // (e.g. - $user = mysql_real_escape_string($_POST['user']);)
    $loginQuery = "SELECT * FROM login WHERE user='".$user."' ".
                   " AND Userpass='".$pass."'";
    //  No need to end with a LIMIT clause

    $loginResult = mysql_query($loginQuery) or
                        die("DB Error" .mysql_error());

    // I do an if () statement because you should only get one result
    // back.  If you get more then one, then I think something is wrong
    if ( mysql_num_row($loginResult) > 0 ) {
      $row = mysql_fetch_assoc($loginResult);
      $_SESSION['user'] = $row1['User'];
      $_SESSION['loggedin'] = "YES";
      $authenticated = "true";
    }
  }
  // Make sure your return is inside the closing bracket
  return $authenticated;
}
?>



--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--- End Message ---
--- Begin Message ---
On Fri, January 25, 2008 12:59 pm, Eric Butera wrote:
> On Jan 25, 2008 1:39 PM, Richard Lynch <[EMAIL PROTECTED]> wrote:
>> On Fri, January 25, 2008 7:27 am, Eric Butera wrote:
>> > I think the memory size can (sort of) be determined by width *
>> height
>> > * bits * channels / 8.  I've never found anything better than
>> that.
>> > There are a few comments on the php site in the gd section about
>> such
>> > things but I've never seen it work 100%.  I always set the real
>> memory
>> > limit much higher than what I say the max is for the memory size
>> just
>> > in case it gets an image that could break it.  At least that
>> stopped
>> > the friendly white screens of death. :)
>> >
>> > If anyone has better ways of handling this I'd love to hear them.
>>
>> Don't try to do the thumb-nail in real-time for the user that
>> uploads it.
>>
>> They already know what the image looks like. :-)
>>
>> Off-load that task to a separate cron job, run from CLI, which can
>> use:
>>
>> php -q -d memory_limit 1000000000 thumbnail.php /path/to/image.jpg
>> /path/to/thumbnails/
>>
>> This way, you are not letting your HTTP php processes chew up more
>> RAM
>> than they really should, and you can give the thumbnail script a GIG
>> (or more) of RAM to do whatever it needs to do.
>>
>> Run that job often enough, and the thumbnail will be available "fast
>> enough" for the users who don't know what the image looks like.
>>
>
> Hi Richard,
>
> The way I handle thumbnail creation is (this is generalized) but an if
> statement that says
>
> if (thumbnail file name not in db) {
>     set img src to a script that renders thumbnail, saves it
> somewhere, and updates the db record so that this never happens again
> until i reset it (in case they want to change thumb sizes)
> } else {
>     raw link to image
> }
>
> The main issue is that when someone uploads an image for a piece of
> content or a product image I need to scale it down to a sane amount of
> ~800 w/h before I even allow it on the site.  From there I use the
> thumbnail on demand.

Perhaps you could check the size of the image in another elseif() in
the middle and handle over-size images specially.

> I'm not sure the cron job idea is the best for my situation though.
> When a client uploads a product image in a store they just paid for
> they expect it to show up before 3am when the server load is down.  If
> I run it every 15 minutes I really don't see the advantage of that
> seeing as I can do an ini_set on the memory limit at any point I
> decide since it is my server.  Cron for 15 minutes still means at 3pm
> which we are at a heavy load the 1g script would kick in.

The difference is that you have one (1) CLI script using 1G, instead
of *EVERY* Apache child using 1G...

You could also run the CLI at nice -n 19, so that it is super low
priority, and then not worry about server load. :-)

Separating the process off from your main logic gives you all sorts of
flexibility that just isn't there when you try to do it inline.

If you are using fast_cgi instead of mod_php, I'm not sure this helps
much in terms of RAM usage...

> The good thing though is that most people just set up their
> store/content and don't really update it too much after that.  That is
> why I'm after a solution that allows me to figure out the memory size
> so I can just tell them if they need to lower the resolution a bit and
> try again.
>
> I'll keep this cron solution in mind though, could come in handy at
> some point.
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
Richard Lynch schreef:

On Fri, January 25, 2008 1:45 pm, Jochem Maas wrote:
Richard Lynch schreef:
On Fri, January 25, 2008 11:33 am, Floor Terra wrote:
I know how to do multiple queries - the key issue in my
question
was how to do them concurrently (i.e. in parallel).
So you want to make PHP multithreaded???
No, just the mysql queries.
Try pcntl_fork() to create a child process for each query.
Each child can make it's own MySQL connection.
Do *NOT* do this in a web-server environment, unless you have
EXTENSIVELY and EXHAUSTIVELY tested all your PHP extensions for
thread-safety!
I agree about the don't do that part - but what has process forking
got to do with thread safety?

AFAIK you shouldn't do that because it madness to fork a complete
apache
process.

Process forking has EVERYTHING to do with thread safety.

Whatever is going to go wrong in a threaded environment is going to
also go wrong when you fork the process, almost for sure.

"Global" variables (or other resources) will not get split/duplicated
correctly, as fork has NO IDEA what needs separating.

So you suddenly have two processing pounding data in/out of what each
of them think is an exclusive resource/memory-address/whatever...

shows what I know :-)


Baby go Boom!


--- End Message ---
--- Begin Message ---

On Fri, January 25, 2008 1:45 pm, Jochem Maas wrote:
> Richard Lynch schreef:
>> On Fri, January 25, 2008 11:33 am, Floor Terra wrote:
>>>>>>> I know how to do multiple queries - the key issue in my
>>>>>>> question
>>>>>>> was how to do them concurrently (i.e. in parallel).
>>>>>> So you want to make PHP multithreaded???
>>>>> No, just the mysql queries.
>>> Try pcntl_fork() to create a child process for each query.
>>> Each child can make it's own MySQL connection.
>>
>> Do *NOT* do this in a web-server environment, unless you have
>> EXTENSIVELY and EXHAUSTIVELY tested all your PHP extensions for
>> thread-safety!
>
> I agree about the don't do that part - but what has process forking
> got to do with thread safety?
>
> AFAIK you shouldn't do that because it madness to fork a complete
> apache
> process.

Process forking has EVERYTHING to do with thread safety.

Whatever is going to go wrong in a threaded environment is going to
also go wrong when you fork the process, almost for sure.

"Global" variables (or other resources) will not get split/duplicated
correctly, as fork has NO IDEA what needs separating.

So you suddenly have two processing pounding data in/out of what each
of them think is an exclusive resource/memory-address/whatever...

Baby go Boom!

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
On Fri, January 25, 2008 1:31 pm, Jochem Maas wrote:
> setup as via register_shutdown_function().

I missed that bit.

Sorry for the noise.

shutdown functions are run outside the normal context of PHP, and need
special care.

Output won't go anywhere, and try/catch won't work, as the bulk of the
PHP "guts" have already gone away.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

--- End Message ---
--- Begin Message ---
wayyyyy offf topic-ish here..

class destructors, surely they differ from the register_shutdown_function in execution style? seeing as one can echo / do a bit of jiggery pokery before the buffers close.

what exactly is the difference?

Richard Lynch wrote:
On Fri, January 25, 2008 1:31 pm, Jochem Maas wrote:
setup as via register_shutdown_function().

I missed that bit.

Sorry for the noise.

shutdown functions are run outside the normal context of PHP, and need
special care.

Output won't go anywhere, and try/catch won't work, as the bulk of the
PHP "guts" have already gone away.


--- End Message ---
--- Begin Message ---
Spatial Joys!

Quite simply I need to unpack wkb binary from mysql back to it's double-precision...

problem is this:
Array
(
    [order] => 0
    [type] => 16777216
    [latitude] => -7.1466311596962E-292
    [longitude] => 1.7568969921627E-47
)
should be:
Array
(
    [order] => 0
    [type] => 16777216
    [latitude] => 3144.1639291789037156
    [longitude] => 4044.3600286609968055
)

when using:
unpack("corder/Ltype/dlatitude/dlongitude", $mysqlwkb);

any idea's just how to get the wkb back to something usable with php?

Nathan

--- End Message ---
--- Begin Message --- If you run the scripts through the CLI you can multithread your queries very easily.. you can wrap it in a while(1) block with a sleep(X) aswell to keep it going forever-ish..

<?php
$child = 0;
$pid = pcntl_fork();
if(!$pid) {
$db = new your_mysql_handler('connection gubbins');
$work = $db->select("select work_to_do from my_database WHERE work_done=0;");
if($work) {
$worker_threads = array();
$jobs = array_chunk($work, 1000, true);
foreach($jobs as $dex => $work) {
$child++;
$workerpid=pcntl_fork();
$worker_threads[$dex] = $workerpid;
if(!$workerpid) {
foreach($work as $ig => $row) {
#VERY important otherwise mysql/php kill your app after a few connections
$db = new your_mysql_handler('connection gubbins');     
#do some work
}
echo "\tworker thread $child complete \n";
exit();
} else {
echo "\tlaunched worker thread $child : ".$workerpid."\n";
}
}
foreach($worker_threads as $dex => $workerpid) {
pcntl_waitpid($workerpid, $status);
}
echo "done the work\n";
}
} else {
echo "\nlaunched my worker";
}
?>

Back on the mysql side of things, try using geometry columns rather than numerical primary keys, with spatial indexes.. it's a MASSIVE performance upgrade (I've cut 5 second queries down to 0.005 by using geo columns)

Nath

Per Jessen wrote:
Jochem Maas wrote:

have you thought of batch processing via cron? you can have a parent
process run off children to handle each long running query and package
the results in format that's quick to output - thereby minimizing the
time taken to batch process and then have the summary page merely use
the latest packaged results.

Yeah, we're already doing quite a bit of this.  We don't do it with
cron, but we cache the long running results for 15min.  That way at
least the summary page appears to be quite responsive (most of the
time).  Excellent suggestion by the way - we are just trying to reduce
this type of batching/caching to be more real-time and/or responsive.


/Per Jessen, Zürich

--- End Message ---

Reply via email to