Log in

View Full Version : Sessions



alexjewell
06-10-2006, 12:17 AM
Ok, here's the deal:
I've created three pages to help me learn sessions.
Page one: index.php
Page two: page2.php
Page three: page3.php
I'll be blunt: I am totally and utterly confused.
The example, http://www.flamehtmlstudios.com/session/.

Ok, here's the code.
Index.php is
just a simple form that submits the person's name to page2.php, which is:



<?php

// PAGE2.PHP

ini_set("bug_compat_42" , "off");
ini_set("session.bug_compat_warn" , "off");

$name = $_REQUEST['name'] ;
if(!$name) {
$name = "You4gotAName";}

setcookie('name' , $name);

session_start();
$_SESSION['name'] = $name ;

?>

<html>
<head>
<title>Session Practice, Page 2</title>

</head>
<body>

<a href="page3.php"><?php echo $name ?>, Go To Page 3</a>.

</body>
</html>


Now, the issue here is that $name is not showing as the person's name, but rather, literally, "$name". Why is this?!

Ok, so, the $name is issue is the same for page3.php, which is below:



<?php

// PAGE3.PHP

ini_set("bug_compat_42" , "off");
ini_set("session.bug_compat_warn" , "off");

$name = $_COOKIE['name'];
if(!$_COOKIE['name']) {
echo "No Name" ;}

session_start();
$_SESSION['name'] = $name ;

echo "

<html>
<head>
<title>Session Practice, Page 2</title>

</head>
<body>

You've reached the end of the line, $name"."!

</body>
</html> ";

unset($_SESSION['name']) ;

?>


Ok, again, the $name issue is present here.
Also, I know I'm supposed to end the session.
How do I do this?

Now, I'm so confused.
First of all, I know I probably don't need the cookie stuff in there at all, since PHP automatically drops a cookie, and if cookies aren't enabled, puts a file with the session id on the server, correct?

Ok, some help here?!
:confused:

mwinter
06-10-2006, 10:51 PM
ini_set("bug_compat_42" , "off");You missed 'session.' from that first string.

The recommended configuration disables that bug. Are you sure you need to do it at run-time?


ini_set("session.bug_compat_warn" , "off");Leaving the warning on won't hurt.


$name = $_REQUEST['name'] ;If data should only be recieved by POST request, use the $_POST superglobal.


if(!$name) {
$name = "You4gotAName";}Ideally, this should be written more like:



$name = !empty($_POST['name']) ? $_POST['name'] : 'Foo';
If the element doesn't exist, attempting to access it will generate a notice in the logs (given the right logging level). The isset function can be used instead of empty if it's more appropriate.

Both of these points apply to the other page, too.


Now, the issue here is that $name is not showing as the person's name, but rather, literally, "$name". Why is this?!I don't see that, nor any obvious reason why it should.


$name = $_COOKIE['name'];
if(!$_COOKIE['name']) {
echo "No Name" ;}You should be reading from the session, not a cookie.


echo "

<html>
Why not jump out of PHP parsing mode, like you did in the previous example?


unset($_SESSION['name']) ;You do this without checking that the element exists. If it doesn't, you code will trigger a fatal error.


Also, I know I'm supposed to end the session.
How do I do this?The abbreviation RTFM comes to mind here. The documentation for the session_destroy (http://uk.php.net/session_destroy) function describes how to invalidate a session entirely.


First of all, I know I probably don't need the cookie stuff in there at all,Correct.


since PHP automatically drops a cookie,If it's configured to do so.


and if cookies aren't enabled, puts a file with the session id on the server, correct?No. Session data - what you add to the $_SESSION superglobal - is (usually) stored in a file on the server. The session identifier is transmitted via cookie (if enabled) and (if permitted), PHP will alter form actions and links if cookies are disabled to accomplish the same. Again, this (session id propagation (http://uk.php.net/manual/en/ref.session.php#session.idpassing)) is described in the documentation.

Mike

alexjewell
06-10-2006, 11:05 PM
Wow, thanks so much for all of that.
I have since fixed these issues (and others) and you can view the full working example by the link above.
Thanks again.
:)

webmasterneo
06-20-2006, 06:26 AM
Wow, thanks so much for all of that.
I have since fixed these issues (and others) and you can view the full working example by the link above.
Thanks again.
:)

hello. am a newbie to php, esp sessions. would you be so kind as to detail to me your working script? i sent you a pm. thanks in advance. :)

webmasterneo
06-20-2006, 06:42 AM
hello. am a newbie to php, esp sessions. would you be so kind as to detail to me your working script? i sent you a pm. thanks in advance. :)

I already got the script to work (except for the last page).. so if you do send me your script, i'll use it for comparison.. thank you again.