Page 1 of 1

How to correctly use sessions

Posted: Thu Oct 13, 2011 1:23 pm
by Tokunbo
hello sirs,

I have been reading about and trying my hand at understanding how sessions work, etc, by following doing my own example.

I have a couple of questions though. This is my code:

Code: Select all

<html>
    <title>My home page</title>
    <body>
    Enter a Name<BR>
    <form method="post" action="my_form1.php">
    	name: <input type="text" name="name" size="20"><BR>
    <input type="Submit" value="Submit">
    </form>
    </body>
</html>

<?php
    $name=$_POST["name"]; // assign input to a variable
   session_start(); //start my session
   $name=$_POST["name"]; // assign input to a variable - just in case?
   session_register("name");  //register a session with input value
  echo "your name is $name"; //echo 

  //store name values in an array  

//   for ($i=0;$i<=100;$i++){
//      $namearray = array($name);
//   echo $namearray;
//    }
?>
For the above code, I made reference to this: http://phptutorial.info/learn/session.php

Questions:
1) I read on another site that "session_start() " is supposed to come first on the code page - no kind of text, nothing whatsoever must come before session_start, etc; does this mean that the above code is wrong? - I mean professionally(codewise)? Im referring to the form in <html></html> - or does the statement refer to ordinary text only?

2) Is my above code really working?...I mean with session_start() part?, etc. Is the way I used it correct? Im asking coz the many examples Ive seen usually are with sessions from one page to another (page1.php to page2.php); besides it is also possible just to re-echo my input even without session-start - I think.

As in, does session_start() work while I am on the same page

example: If I were to enter: dog, cat, mouse and hit the submit button for reach, I can see what I enter...and if after the third "mouse", I click the "back" button on my browser, I can see the previous entries - but Im wondering if what Im seeing isnt just the browser's history or? Im kinda confused.

Pls help to explain.

So, to resolve my confusion, I wanted to output whatever was being inputted in some sort of table, so that after entering, dog, cat and mouse, I can see a list"1-3, displaying dog, cat and mouse'; then maybe I could have an option to delete cat, and remain just dog and mouse. This is where I got stuck and had to comment out the code.

pls help / advise / explain
Tokunbo

Re: How to correctly use sessions

Posted: Thu Oct 13, 2011 2:20 pm
by pickle
1) Yes, session_start() must come before any output. This is because session_start() sends headers, which cannot be sent once output is generated. In your code, move session_start() to the very top.

2) Not sure what you're trying to accomplish with your code. If you want to store the name in session, you'd do something like:

Code: Select all

$name = $_POST['name'];
$_SESSION['name'] = $name;
session_register() hasn't been necessary for a long time & is deprecated in 5.3. Don't use it.

Re: How to correctly use sessions

Posted: Thu Oct 13, 2011 5:17 pm
by Tokunbo
@pickle,

you mean something like this:

session_start(); //start my session
<?php
$name=$_POST["name"]; // assign input to a variable

$name=$_POST["name"]; // assign input to a variable - just in case?
session_register("name"); //register a session with input value
echo "your name is $name"; //echo

//store name values in an array

// for ($i=0;$i<=100;$i++){
// $namearray = array($name);
// echo $namearray;
// }

<html>
<title>My home page</title>
<body>
Enter a Name<BR>
<form method="post" action="my_form1.php">
name: <input type="text" name="name" size="20"><BR>
<input type="Submit" value="Submit">
</form>
</body>
</html>

?>
what I want to achieve is this: I have an input box and a submit button. when a user enters his name in the input box and clicks the submit buttion, I want to output whatever was being inputted in some sort of table, so that after for ex: entering, dog, cat and mouse, I can see a list"1-3, displaying dog, cat and mouse'; then maybe I could have an option to delete cat, and remain just dog and mouse. This is where I got stuck and had to comment out the code.

Re: How to correctly use sessions

Posted: Thu Oct 13, 2011 5:27 pm
by pickle
No - you're still using session_register(). See the code I posted.

I don't know how to do what you ultimately want - I was just commenting to help you use sessions properly.