Link to home
Start Free TrialLog in
Avatar of Ray Paseur
Ray PaseurFlag for United States of America

asked on

Session-Related Logic Error

I need another pair of eyes on this problem.  I have two scripts identical except for the instruction on line 34.  One works and one does not.  You can install the code here and run it to see the effect in action.  On my server, the apparent error is that the Session array does not hold the "cv" value if the value was generated by rand().

This one works as expected:
<?php // RAY_temp_session_works.php
error_reporting(E_ALL);

// ALWAYS START SESSION UNCONDITIONALLY
session_start();
if (empty($_SESSION["cv"])) $_SESSION["cv"] = '?';

// SET FORM STRING
$str = <<<'EOCAP'
<pre>
<form method="post">
ENTER 'CCC' IN THIS BOX:
<input name="cv" value="" autocomplete="off" />
<input type="submit" />
</form>
EOCAP;

// WAS THE FORM SUBMITTED?
if (!empty($_POST["cv"]))
{
    // SHOW SESSION AND POST
    echo "<pre>";
    echo "SESSION: ";
    var_dump($_SESSION);
    echo "POST: ";
    var_dump($_POST);

    // TEST FOR MATCH
    if ($_POST["cv"] == $_SESSION["cv"]) echo PHP_EOL . 'MATCH';
    if ($_POST["cv"] != $_SESSION["cv"]) echo PHP_EOL . 'NO MATCH';
}

// GET A NEW FIXED VALUE
$cv = '321';

// STORE THE VALUE IN THE SESSION
$_SESSION["cv"] = $cv;

// AND PRODUCE THE FORM
$new = str_replace('CCC', $cv, $str);
echo $new;
var_dump($_SESSION);

Open in new window


This one does not work as expected:
<?php // RAY_temp_session_error.php
error_reporting(E_ALL);

// ALWAYS START SESSION UNCONDITIONALLY
session_start();
if (empty($_SESSION["cv"])) $_SESSION["cv"] = '?';

// SET FORM STRING
$str = <<<'EOCAP'
<pre>
<form method="post">
ENTER 'CCC' IN THIS BOX:
<input name="cv" value="" autocomplete="off" />
<input type="submit" />
</form>
EOCAP;

// WAS THE FORM SUBMITTED?
if (!empty($_POST["cv"]))
{
    // SHOW SESSION AND POST
    echo "<pre>";
    echo "SESSION: ";
    var_dump($_SESSION);
    echo "POST: ";
    var_dump($_POST);

    // TEST FOR MATCH
    if ($_POST["cv"] == $_SESSION["cv"]) echo PHP_EOL . 'MATCH';
    if ($_POST["cv"] != $_SESSION["cv"]) echo PHP_EOL . 'NO MATCH';
}

// GET A NEW RANDOM VALUE
$cv = (string)rand(101, 999);

// STORE THE VALUE IN THE SESSION
$_SESSION["cv"] = $cv;

// AND PRODUCE THE FORM
$new = str_replace('CCC', $cv, $str);
echo $new;
var_dump($_SESSION);

Open in new window


Thanks for your help, ~Ray
Avatar of dsmile
dsmile
Flag of Viet Nam image

Are you sure $str is well defined?

$str = <<<'EOCAP'

Parse error: parse error, unexpected T_SL in xxx.php on line 9
After I corrected that $str, I've got your script run.
And both scripts run fine as designed.

Don't know how your server configured but I suggest that you change each keyname of POST and SESSION that represents cv to different names so that there're no chances that php engine might get confused.
Avatar of Ray Paseur

ASKER

$str = <<<'EOCAP'

This is NOWDOC syntax.  PHP 5.3+
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

Can you please post a link to where you have the scripts installed?  I would like to observe.  My versions are here:
http://www.laprbass.com/RAY_temp_session_works.php
http://www.laprbass.com/RAY_temp_session_error.php

Thanks, ~Ray
Sorry, I didn't know that. I'm still using PHP 5.2

I don't have a host so I can't show you my deployment of your scripts.
But when I access your online versions, they still work fine.

I use FF 3.6.12 and IE8.
ray-session-ie.JPG
Interesting.  I deleted my cookies, and reran the test.  It worked correctly on IE8 and failed on FF 3.6.12.  Did you actually test FF 3.6.12?  I noticed that your posted image was IE.

Thanks for your help here.  Still not sure what to make of this.
Yes I did, Ray, just forgot to attach the screenshot.

If deleting cookies makes it run again, then a little tweak on session configuration in php.ini might help.
Let me know when you find the root cause :)
ray-session-ff.JPG
No, deleting the cookies did not make it run correctly.  I'm mystified.  I've reproduced the failure on a PHP 5.2 server, so I am beginning to think there is something wrong with my installation of FF.
ASKER CERTIFIED SOLUTION
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's mine.
about-config.png
And while it works for you, I am still getting the error from this URL
http://www.laprbass.com/RAY_temp_session_error.php

And I am still amazed!
error.png
Do you still get it from my URL Ray, or is it just yours?
http://www.patsmitty.com/ExpertsExchange/temp_session_error.php

If you only get the error on yours then I would assume that your php settings have to be the culprit. But if you get the error on both, then re-install your FF and see if that doesn't fix it.

Could this be a caveat to the good quote: "To err is human, and to blame it on a computer is even more so"?  haha
See attached.  I'm not exactly a novice in computer science and I am completely baffled.  I'm at PHP Version 5.3.4.  This is the only session-related error I have been able to cause.  I'll probably re-install FF, but I sure would like to know how the error is occurring.
patsmitty.png
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
temp_session_error.php - NO MATCH
temp_session_error_1.php - NO MATCH

Thanks.  I may take this mystery to the grave.
Never debugged the issue, but it went away when I upgraded Firefox, so I am a happy camper.  @patsmitty: Thanks for your help.  I, too, can't think of any reason the fixed value would work where the random value wouldn't.
I guess I just don't understand how the browser could corrupt sessions somehow. Maybe, just maybe a plugin/add-on that you had was interfering. Thanks for the points though.

Cheers
Yeah, I don't understand it either.  Probably something about returning the wrong cookie.  Thanks for your help just the same!