Jump to content

trouble understanding sessions


chopficaro

Recommended Posts

ive read about sessions on w3schools and php.net and im still having trouble understanding them. here is some text from php.net:

 

 

 

The session support allows you to register arbitrary numbers of variables to be preserved across requests.

 

so if a user visits page A, does some stuff and i have php save the stuff they did in a variable, and then they click on a link to another page B on the site, i can use the saved values from page A to do stuff on page B right? is that the point of a session? what kind of stuff are we dealing with?

 

 

 

Session support in PHP consists of a way to preserve certain data across subsequent accesses.

 

what defines a subsequent access? can you close your browser, upen it again and visit the same page and still be in the same session?

 

 

 

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

 

can i controll weather it is used in a cookie or in the url? how does it look when it is in the url?

Link to comment
https://forums.phpfreaks.com/topic/187319-trouble-understanding-sessions/
Share on other sites

Session are unique to the user and is available as long as the session is valid. Any page that will use session data must start with:

session_start();

regardless if the session have already been use earlier. So...

 

page1.php

session_start();

//some form stuff

$_SESSION['user'] = $_POST['user'];

 

page2.php

session_start();

echo "welcome {$_SESSION['user']} to page 2";

 

About as straight forward an example as I can think of. Note how even though we started the session on page1 and saved the username from the form to the session data we had to start the session again on page2 in order to access the session data again. You can add or alter session data on any page of your site as long as you start the page with session_start().

 

As a further example:

 

page3.php

session_start();

$_SESSION['user'] = 'joe';

 

Even if the person who started the session was registered on page1 with the username of dave they would now be know as joe.

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.