Jump to content

Php Session Help


batofd

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.