How to correctly use sessions
Posted: Thu Oct 13, 2011 1:23 pm
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:
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
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;
// }
?>
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