You are invited to Log in or Register a free Frihost Account!

PHP Sessions

Hey guys, I was planning to create a script in PHP that uses session variables, now this is my question. Is it possible to have more than one session variable in one page that each of the variable has different values from each other?

-- Please close this topic if you find this nonesense. Thanks to those who will reply. Smile
Yes, you can have multiple session variables and values on one page. Wink

$_SESSION['some1'] = "something111";
$_SESSION['some2'] = "something222";
$_SESSION['some3'] = "$somethingOther";

ah, how do I register a session variable so that I can test it or display in on a different page? and how do I unregister it if I don't want it to appear?
well you register sessions as above which is pretty simple. To unregister you use the session_unregister function. You dont need to unregister them though so I wouldnt bother.

To display a sessions value use
echo $_SESSION[name'];

If you dont want to display the sessions value dont do anything with it.

For other pages they need to be php pages and have the session_start() function at the top. They should pass automaticly but you can change the way its passed from cookies to url etc and back and forth. (Go down to 'Passing the Session ID')
thanks for the reply. another question, is the session automatically ends when the browser is closed?
you really got your answer right away huh lol Very Happy
yah, but I have more questions to come..
This is what I needed thanks a lot!
I had only used cookies and this might also be good help. Must try everything Wink

So thaks a lot!
Yes, session automatically ends when the browser is closed, but is quite clever to destroy it on the end of PHP script or in situation when something going wrong.

if($something != 3459) {

how can I make a session exist after I close the window and when I open a new window and access my page the session is still there? or is it possible?
Nope, that is not possible.. what you want to do sounds like a perfect candidate for cookies..

Sessions are used to track visitor data during a single connection and across page changes within the same application..

Cookies are used to track visitor data during multiple connections within the same application.. Cookies can be utilized to maintain data when a user closes their browser and opens it again acessing your site again..

Sessions are stored on the server, while cookies are stored on the client..

I hope that helps...
Does the php page need to have the session_start() function at the top? I know that I a never put session_start() at the start of my pages anymore and the session variables still seem to function like they should. I think I started running into errors saying that 'headers already sent', so I stop putting the session_start() function at the top of the page and it got rid of the error. What is the session_start() function for? What does it do? is it bad that I leave it out?
Some servers have Session Auto-Start enabled, so you don't need to eenable sessions manually, but that is bad practice in my opinion, and you should always code with session_start() in your php applications.. For one, if Session Auto-Start is disabled, then your sessions will still be started manually, and second, if Auto-Start is enabled, then the session_start() function does nothing (except throw an error if your headers have already been sent)..

And to avoid your headers being sent before your PHP script finishes, you should make sure there is NO white space before <?php and after ?> in any of your script files.

If you have mingled code (like HTML and PHP in the same file) then shame on you! But you can still just keep the very first portion of the file as a PHP code block with NO white space before <?php and start your sessions within the first code block...

Oh, and to turn Session Auto-Start off, you can add this to the beginning of your code:


ini_set( 'session.auto_start', false );

(Or just edit the php.ini file, and alter it and the source, but we don't have access to that here on Frihost, and we don't need access to it.. use the code snippet above!)

Anyway, I hope that helps..
Thanks. that does explain a lot. I knew that it must've been bad practice to leave it off. but I was a newbie when I started getting those headers already sent errors and that was the only thing that i could think of to fix it at the time. then i just got out of the habbit of it. Thanks again.


I'm having trouble ending the session...

I allways get the same session name, and can't change it everytime I login...
You should just post the whole script (if its not too big)...
Related topics
php sessions
PHP: Sessions and Cookies
php sessions
problems for indexing php pages in google.
PHP sessions and security
How to make a log in log out system for a website??
php sessions for different accounts
php SESSIONS question
$_COOKIE and $_SESSION help needed!
friendster issue
Trying to start out!!!
is there session in perl
PHP Sessions Issue
PHP Session Timeout/Max Life Time
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.