User Contributed Notes: session_start
gburningham@yahoo.com
01-Dec-1999 12:30
How much information is 'session' storing? Can this be put into a database? Does 'session' store internal progress in a page? I am currently looking at trying to track internal links without the aid of cookies and i think this is what i am looking for. Does anyone know the in's and out's of a php 'session'?
mhazlett@fragnet.net
11-Dec-1999 12:51
I have found that if you create a session with session_start() and you don't close it, it becomes persistent threw out all that clients connections. (You can't get rid of it eather). I had to take all refrences to session_start and session_destroy out to get it to work like it does in the docs.
theo@alldarlings.com
09-Jan-2000 01:54
<PRE>
What session_start does is looks for an existing session, and if none exists, it creates a new one. What it also does (that wasn't fully eplained above by the statement "initializes variables" is looks for any session variables, and sets them globally on the page that start got called from. If you have any variables already set (say from a get/post from the previous page) they will get overwritten. Also, changing a variable on a page will not update its session counterpart.
Also, note that any call to session_register will automatically call session_start (and therefore pull all session variables and set their local counterparts.)
If I wasn't so lazy, I'd upload an example of what I mean.
</PRE>
robp@wincom.net
24-Feb-2000 07:32
How about a way to change the parameters of the HTTP cookie that's sent when a session starts? I'd like to be able to set the "domain" variable to "*.domain.com", since in my shopping cart system, the user might be browsing between "www.domain.com" and "secure.domain.com".
mike@4host.com
28-Feb-2000 04:11
The cookie's variables are settable.
Look in the php.ini file under the [Session] header.
robp@wincom.net
01-Mar-2000 05:24
Sure, but setting this in the php.ini file results in a global change. I'd like to be able to set the session.cookie_path and session.cookie_domain variables on a per use basis, in the actual PHP code. I have multiple sites running on my server so using one domain wouldn't help at all.
hunter@lastonepicked.com
17-Mar-2000 01:50
I wish that session_start() was session_start(boolean createNewSession)... Is there a way to see if the client has a current session without creating one if they do not?
jay@wisepay.com
09-Apr-2000 08:36
Why do I get this error when I try to start a session with session_start();
Warning: Cannot add header information - headers already sent in /home/httpd/html/jay/bin/login.php on line 13
nehresma@yahoo.com
09-Apr-2000 08:50
I run into that header thing a lot when I have whitespace (or anything else) outside of the php script tags. Check your included files for this too.
elsalsero@writeme.com
13-Apr-2000 06:02
"Cannot add header information - headers already sent in ..."
You have to place your session_start() in the header or better over the header. It must be the first function.
duddy@svivot.com
27-Apr-2000 07:24
Hi, I don't understand why anything I typed AFTER the session_start() function is not executed (including HTML outside the script).
It looks like the file is simply stucked after it reached that function.
I am using the PHP4RC1.
waiting for anyone to help me...
David
bryan_w@acm.org
04-Jul-2000 05:14
Duddy -
I had this problem when I was running the non-cgi win32 executable (isapi?). After switching to the win32 executable session functions started working.
giridhar@vasnet.co.in
14-Jul-2000 04:15
can we encode sessionid to the url of a static html page??? if so some one please let me know how to do it...
thankx in advance.
giridhar
nookko@hanmail.net
18-Jul-2000 01:34
why does programmar use SESSIONID for the pupose of structing shoppingmall?
ryan@newintellectual.com
24-Jul-2000 07:22
I had sessions working last night and now I have this error. It happens when I try to start a new session.
Warning: open(/temp/sess_2d36259385b3d77ca83206afe356e26d, O_RDWR) failed: Permission denied (13) in /usr/local/apache/htdocs/test/session_test.php on line 2
ryan@newintellectual.com
24-Jul-2000 08:19
Never mind, I just had to make the temp directories permissions to 777 and now it works.
arcturus@arcy.org
26-Jul-2000 10:42
If you start session with 'session_start()', and open session again in another page without closing previous one, the second page will not open before close first one.
In shout word, you cannot start two session at the same time.
rayhar@kc.rr.com
05-Aug-2000 06:17
If you've read the newly published SAMS book, "Teach Yourself PHP4 in 24 Hours" by Matt Zandstra (the other PHP4 books are not yet published), and the documentation on PHP's web-site, and became as frustrated as I did trying to get "sessions" to work, I hope my description here will help.
The example on page 340 in SAMS book does not work.
A simplified example (according to the book) is:
Here's the first page:
testsession1.php:
=====================================================
<?php
session_start();
session_register("fname");
session_register("lname");
$fname = "Mickey";
$lname = "Mouse";
print session_id()." ";
print session_encode()." ";
print session_save_path()." ";
?>
=====================================================
Browsing testsession1.php sets the session variables.
Here is what is displayed in Browser (testsession1.php)
6ef4f3c6ff1bab0d7b1f274c2f538c5e
fname|s:6:"Mickey";lname|s:5:"Mouse";
/tmp
Here's the next page:
testsession2.php
=====================================================
<?php
session_start();
print $fname;
print " ";
print $lname;
print session_save_path();
?>
=====================================================
Supposedly, browsing the second page will get and display the session variables. However, it does not work. A separate session is started.
Here is what is displayed in the browser (testsession2)
/tmp
Looking at the "/tmp" directory on the Linux/Unix server reveals what happened in the directory:
Here is the result from the first "testsession1.php" page:
[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:41 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e
Here is the result from the second "testsession2.php" page:
[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:44 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e
-rw------- 1 httpd httpd 0 Aug 4 22:44 sess_82a894a3816e83f38268fd36695fccc2
You can see that the second page did not access the original session and its variables. It created a new session and hence a new session/cookie/file on the server.
*** HERE IS WHAT WORKS *** -- the example on page 345 of SAMS book:
Here is the first page...
testsession1.php:
=====================================================
<?php
session_start();
session_register("fname");
session_register("lname");
$fname = "Mickey";
$lname = "Mouse";
print session_id()." ";
print session_encode()." ";
print session_save_path()." ";
?>
<A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2Fhu%2F%26quot%3Btestsession2.php%3F%26amp%3Blt%3B%3F&y=1999 print SID; ?>">Next Page</A>
=====================================================
Here is the second page...
testsession2.php:
=====================================================
<?php
session_start();
print $fname;
print " ";
print $lname;
print " ";
print session_save_path();
?>
=====================================================
When you click on the link to the next page, here is the ADDRESS in the browser:
<WEBSITE_URL>/testsession2.phtml?PHPSESSID=d66198107f6cfaa1dee0f657a286feb2
Here is what is displayed in the browser.....
Mickey
Mouse
/tmp
As you can see, the variable "SID" was magically changed to the variable "PHPSESSID", and the second page magically opened the session with the session_start() function.
I hope this helps.
ferry.haryadi@rtm.co.id
14-Aug-2000 11:38
regarding example above.
<A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2Fhu%2F%26quot%3Btestsession2.php%3F%26amp%3Blt%3B%3F&y=1999 print SID; ?>">Next Page</A>
why the link become "testsession2.php?" the SID not working.
btw, i am use PHP4 for win32
regards,
Ferry.