First of all i am new to PHP Session.
THE PROBLEM:
I do session_start(); command at the begening of every page so that php uses session but what i also what it to do is that if that person closes the browser and returns back to same website it should atleast pick up the person's user name so that they dont have to type it again and again when ever they come back. plus to that every single time that person close the browser and come back to that website it creates a new session which is painful as each session take space on the webserver. any idea on how to get the php to get the session id of an old session that was made by the same website and run that session if it is available. any tips on session is good as i was not able to find good documentation on php.net regarind this problem.

I think you should use cookies. I also think you should look at this script. The top of this page has a great php login with session handling and cookies for automatic re-login. I have the link here though to my comment on the script that will have some extra stuff

http://www.mt-dev.com/2002/07/creating-a-secure-php-login-script/#comment-4746
<go to top of page to see origonal code>

The original code is good but requires a PEAR connection to a mysql db. I don’t use PEAR so I re-wrote the script with some small changes. that’s why I want you to go to my comment because you can download the whole class instead of putting their puzzle peaces together

Brad

From Zend:

==
Chapter 35. Cookies

PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the setcookie() or setrawcookie() function. Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser. This is the same limitation that header() has. You can use the output buffering functions to delay the script output until you have decided whether or not to set any cookies or send any headers.

Any cookies sent to you from the client will automatically be turned into a PHP variable just like GET and POST method data, depending on the register_globals and variables_order configuration variables. If you wish to assign multiple values to a single cookie, just add [] to the cookie name.

In PHP 4.1.0 and later, the $_COOKIE auto-global array will always be set with any cookies sent from the client. $HTTP_COOKIE_VARS is also set in earlier versions of PHP when the track_vars configuration variable is set. (This setting is always on since PHP 4.0.3.)

For more details, including notes on browser bugs, see the setcookie() and setrawcookie() function.

==

Furthermore, check out the setcookie function information provided by PHP.NET.

A few examples that may help you start in the right direction (taken from the setcookie php.net link above):

Example 1. setcookie() send example  <?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>
Example 2. setcookie() delete example  <?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>
Example 3. setcookie() and arrays  <?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
	    echo "$name : $value <br />\n";
    }
}
?>

Past that, try googling for 'PHP cookie' or 'PHP session' tutorials. ;)

Thanks for all the help i will try all of these examples and see what works the best..

This article has been dead for over six months. Start a new discussion instead.