batofd Posted July 6, 2007 Share Posted July 6, 2007 I'm kinda new to the session thing with php and ran a test. Code is below - <?php session_start(); $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; setcookie('sessionid',"12365489798798797", time()+600, $_SERVER['PHP_SELF'], $domain, false); echo $_COOKIE[sessionid]; ?> Why when I go to the page it prints nothing until I refresh once? Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 You don't need to set the cookie. Using session_Start does it for you as long as session_use_cookies (or something like that) is set. <?php session_start(); if (!isset($_SESSION['test'])) { $_SESSION['test'] = "This is a test"; echo '<a href="' . $_SERVER['PHP_SELF'] . '">Click here to reload</a>'; }else { echo 'It freaking worked! Test is ' . $_SESSION['test']; } ?> Nice little test script. Quote Link to comment Share on other sites More sharing options...
batofd Posted July 7, 2007 Author Share Posted July 7, 2007 I know that I dont have to set the SID cookie as session_start() does this. I'm saying is; even using the test script; why when I access the page it takes a refresh of the browser to print the $_SESSION or $_COOKIE variable. I put your test script in a file called sess.php and went to the link. I checked my browser cookies and PHPSESSID was set as a cookie. It still showed up as "Click here to reload". Until I refreshed my browser the $_SESSION variable was not displayed. What I'm really trying to do is make a login script that stores a unique cookie which is stored in a MySQL database for that particular user. Thing is, the client can login but it takes either them clicking on another link inside the application or them refreshing the browser for the $_COOKIE or $_SESSION variable to either be outputted or properly stored. How can it echo the variable the first time without the refresh? Let me know if this makes sense or if you need an example for further discussion. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 7, 2007 Share Posted July 7, 2007 Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE That should explain why you have to wait to access the cookie until the page reload. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.