Jump to content

$_SESSION problem


naddie

Recommended Posts

Hi, I've searched as much as I could and couldn't find an answer to this.

 

I'm currently using the latest version of WAMP Server (2.0i).

 

I'm trying to figure out how to use $_SESSION variables, and either passing them to another page, or using them on the same page again after a refresh.

 

With this simple example from a tutorial I read:

 

<?php

session_start();

 

if(isset($_SESSION['views']))

    $_SESSION['views'] = $_SESSION['views']+ 1;

else

    $_SESSION['views'] = 1;

?>

 

With this code, the counter should update on a page refresh.  But it stays at 1.

 

session_start() is there, and I get no errors when I refresh the page or send it to another page.

 

Is there something I'm missing or does something need to be configured in WAMP for it to work?

 

Any help would be appreciated, thanks!

 

 

Link to comment
Share on other sites

Hi, I've searched as much as I could and couldn't find an answer to this.

 

I'm currently using the latest version of WAMP Server (2.0i).

 

I'm trying to figure out how to use $_SESSION variables, and either passing them to another page, or using them on the same page again after a refresh.

 

With this simple example from a tutorial I read:

 

<?php

session_start();

 

if(isset($_SESSION['views']))

    $_SESSION['views'] = $_SESSION['views']+ 1;

else

    $_SESSION['views'] = 1;

?>

 

With this code, the counter should update on a page refresh.  But it stays at 1.

 

session_start() is there, and I get no errors when I refresh the page or send it to another page.

 

Is there something I'm missing or does something need to be configured in WAMP for it to work?

 

Any help would be appreciated, thanks!

 

Try running this code, if you get the "setting it to 1" message more than once... well, i dont know lol.

<?php 
session_start();

if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else 
    $_SESSION['views'] = 1;
    echo "variable wasnt set... setting it to 1";
?>

 

Link to comment
Share on other sites

I get no errors

 

Do you have error_reporting set to E_ALL (or higher) and display_errors set to ON in your master php.ini and you have confirmed what the settings actually are using a phpinfo(); statement (in case the php.ini that you are changing is not the one that php is using)?

Link to comment
Share on other sites

<?php 
session_start();

if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else 
    $_SESSION['views'] = 1;
    echo "variable wasnt set... setting it to 1";
?>

 

You have no brackets on your if. You can go without brackets but only the first statement after will be process as part of the if. Thats why you always see the echo in the else cause without brackets its by default not part of the if /else statement.

 

When you have problems why not try echo out the var in question. You will see that it probably does increment.

 

 

HTH

Teamtomic

Link to comment
Share on other sites

That was juanpablo's suggestion above.  I used the brackets when testing which part of the if/else is true.  After the if/else, I echo'd the variable by itself which is how I know its not incrementing.  I just says "1".

 

When I try to send the variable to another page it says "undefined index ... etc"  I know said I had no errors in my original post, but I misspoke, I guess meant no errors when refreshing the page.  I DO get an error when sending it another page, showing that it's not set.

Link to comment
Share on other sites

<?php 
session_start();

if(isset($_SESSION['views']))
{
    $_SESSION['views'] = $_SESSION['views']+ 1;
echo "$_SESSION['views'];
}
else
{ 
    $_SESSION['views'] = 1;
    echo $_SESSION['views'];;
}
?>

 

Put this in. cut and paste it. Refresh the page a few times. what happens?

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Check the rest of the page. Do you somewhere on it maybe reset it? Cause what you are describing should not be happening. If its set to one it should be passing the isset and increment.

 

If you do not reset the var somewhere then your sessions are not working. What does it say about sessions with a phpinfo()?

Link to comment
Share on other sites

All that's in the code right now is what you posted for me.  And the "1" is the only output.

 

I don't really understand the session info from phpinfo().. I'll paste it here:

 

Session Support enabled

Registered save handlers files user

Registered serializer handlers php php_binary wddx

 

Directive Local Value Master Value

session.auto_start Off Off

session.bug_compat_42 On On

session.bug_compat_warn On On

session.cache_expire 180 180

session.cache_limiter nocache nocache

session.cookie_domain no value no value

session.cookie_httponly Off Off

session.cookie_lifetime 0 0

session.cookie_path / /

session.cookie_secure Off Off

session.entropy_file no value no value

session.entropy_length 0 0

session.gc_divisor 1000 1000

session.gc_maxlifetime 1440 1440

session.gc_probability 1 1

session.hash_bits_per_character 5 5

session.hash_function 0 0

session.name PHPSESSID PHPSESSID

session.referer_check no value no value

session.save_handler files files

session.save_path c:/wamp/tmp c:/wamp/tmp

session.serialize_handler php php

session.use_cookies On On

session.use_only_cookies On On

session.use_trans_sid 0 0

 

(apologies, not sure how to put it into a code box or anything)

 

I thought maybe something is messed up in the settings, since the code looks right.. but I'm not sure what should be enabled etc.

 

Thanks for the help, btw!

Link to comment
Share on other sites

<?php

session_start();

 

if(isset($_SESSION['views'])) {

$_SESSION['views'] = $_SESSION['views']+ 1;

echo $_SESSION['views'];

}

else {

    $_SESSION['views'] = 1;

    echo $_SESSION['views'];

}

?>

 

This is the current code from a previous poster (I don't know how to put it in a code box).  It's just a simple example to see if sessions are working.

 

The echo $_SESSION['views']; is what is displaying the value.

Link to comment
Share on other sites

Okay, I added those, so this is the current code:

 

<?php 
ini_set("display_startup_errors", "1"); // I suspect this setting will reveal the cause of the problem
ini_set("display_errors", "1");
error_reporting(E_ALL);

session_start();

if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views']+ 1;
echo $_SESSION['views'];
}
else { 
    $_SESSION['views'] = 1;
    echo $_SESSION['views'];
}
?>

 

And it's still displaying just the '1' after multiple refreshes.

Link to comment
Share on other sites

Okay, silly me.  I added an exception for localhost, and it's now working.  Thank you!  I apologize, I should have tried that.  I really thought they were different.

 

One last question if you don't mind, and I'll stop bugging you guys.  Is it possible to use session variables without cookies?  If so, what settings need to be changed?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.