Page 1 of 1
Getting values from sessions
Posted: Tue Oct 01, 2002 9:51 am
by mikebr
Now I have had the cookie questions solved I have discovered a problem getting the values from a "session", the problem I have here is getting the value from "the cookie" after a session has been set, the book tells me:
Code
$display="Employee Name : $name";
but now "I guess with the register_globals changes" this does not work so I tried the following:
<?php
$display="<PRE>
Employee Name : $_SESSION[name]<br></br>
Department : $_SESSION[dept]<br></br>
Pay Grade : $_SESSION[pay_grade]<br></br>
Location : $_SESSION[location]<br></br>
Home Address : $_GET[address]<br></br>
Telephone : $_GET[telephone]<br></br>
Employee ID : $_SESSION[employee_no]<br></br></pre>";
?>
but "$_SESSION[variable]" does not seem to return a value, I understand from the documentation at php.net that this seems to be the way to get the value from a session variable!
There are three files to this example, the variables above that I have used $_SESSION on are variables set with the first page of the form then the ones I have used GET on are the variabled set with the 2nd form/page "Which return there values OK", the code above is taken from the 3rd form which returns the values set for the session with forms 1 and 2. I have also tried "$_COOKIE" just in case but there is no value returned there either. I hope this is clear enough to be understood!
Mike
Posted: Tue Oct 01, 2002 9:53 am
by nielsene
Can you please show the code that should be setting the session variables?
Posted: Tue Oct 01, 2002 9:56 am
by Coco
did you start_session()?
Posted: Tue Oct 01, 2002 10:25 am
by mikebr
OK, The code consists of 3 files ss1.php calls ss2.php calls ss3.php.
---CODE ss1.php
<?php
# ss1.php
session_start();
$employee_no=session_id();
session_register("name","dept","pay_grade","location","employee_no");
?>
<html>
<body>
<?php
$form="<center><h2>Delivery etc - New Employee</h2></center>
<form method=GET action=\"ss2.php\">
<b>Employee name:</b><br></br>
<input type=\"text\" name=\"name\" size=20>
<br></br><b>Department</b><br></br>
<select name=\"dept\">
<option selected>Delivery Driver
<option>Warehouse
<option>Accounts
<option>Administration
</select>
<br></br>
<b>Pay Grade</b><br></br>
<select name=\"pay_grade\">
<option selected>Top Job
<option>Normal
<option>Hope Employee Likes Overtime
<option>Does Not Even Register
</select>
<br></br>
<b>Country Location</b><br></br>
<select name=\"location\">
<option selected>USA
<option>England
<option>Germany
</select>
<br></br>
<hr>
<input type=\"submit\" value=\"Next Page\">
<input type=\"reset\" value=\"Clear\">
</form>";
?>
</body>
</html>
<?php
echo $form;
?>
---CODE ss2.php
<?php
# ss1.php
session_start();
session_register("address","telephone");
?>
<html>
<body>
<?php
$form="<center><h2>Delivery etc - New Employee</h2></center>
<form method=GET action=\"ss3.php\">
<b>Employee Addresss:</b><br></br>
<input type=\"text\" name=\"address\" size=30>
<br></br><br></br><b>Employee Telephone No:</b><br></br>
<input type=\"text\" name=\"telephone\" size=30>
<br></br>
<hr>
<input type=\"submit\" value=\"Next Page\">
<input type=\"reset\" value=\"Clear\">
</form>";
?>
</body>
</html>
<?php
echo "$form";
?>
---CODE ss3.php
<?php
# ss3.php
session_start();
?>
<html>
<center><b><h3> Here are the new employee details</h3></b></center>
<?php
$display="<PRE>
Employee Name : $name<br></br>
Department : $_SESSION[dept]<br></br>
Pay Grade : $_SESSION[pay_grade]<br></br>
Location : $_COOKIE[location]<br></br>
Home Address : $_GET[address]<br></br>
Telephone : $_GET[telephone]<br></br>
Employee ID : $_COOKIE[employee_no]<br></br></pre>";
?>
</body>
</html>
<?
echo "$display";
$to="admin@127.0.0.1";
$header="New Employee Addition";
$info= "Here are the details...
NAME: $_COOKIE[name]
DEPARTMENT: $_COOKIE[dept]
PAY GRADE: $_COOKIE[pay_grade]
HOME ADDRESS: $_GET[address]
TELEPHONE NO: $_GET[telephone]
EMPLOYEE ID: $_COOK[employee_no]";
mail($to,$header,$info);
session_destroy();
?>
ps, how do I get this to colour code when showing php examples?
Thanks
Mike
Posted: Tue Oct 01, 2002 10:34 am
by nielsene
OK, it appears you are a little confused on how sessions work.
The general format of using sessions is to:
Step 1: Create the data input forms which use either GET/POST
Step 2: Process the GET/POST data
Step 3: Stored the process and selected data into a session
Step 4: Retrieve session data on later pages
So what does this mean for you, I'm going to walk through the pages quickly. Let me know if this doesn't provide enough information to get started:
Page 1: ss1.php, you can remove all the session stuff
Page 2: add lines like $_SESSION["name"]=$_GET["name"]; to register the passed variables. You'' want to keep the session_start() a the first line. If you want to do any error checking on the form inputs, do that before you save teh GET values to SESSION. What this will do for you is basically avoid the need to use hidden form fields to pass ss1.php's data to ss3.php via ss2.php. You can also remove the session_register("address","telephone") line from ss2.php.
Page 3: so telephone and address are still in teh GET array, the other values are in SESSION. You can move teh GET variables to SESSION if needed as shown in page 2, or just use the appropriate array when you need the data.
Does this all make sense?
Posted: Tue Oct 01, 2002 11:17 am
by mikebr
OK, I will try and get my head around this and then come back.
by the way, this is not my way of creating this process, this is an example from a book on dynamic web sites with php and MySQL that I am using to learn how to use php and MySQL, so it is an exercise on showing the reader how the session thing works.
Mike
Posted: Tue Oct 01, 2002 12:08 pm
by mikebr
I think I understand this a little better now, the changes you told me to make gave me the results with ss3.php that where expected. I have pasted the revised code below, I take it I can add variables to the "session" from another page using $_SESSION ? this I will try. There is one thing I noticed and that is when the "Submit" is pressed on ss2.php it takes as much as 60m seconds for ss3.php to load and that is from localhost, is this not a long time for what appears to be a simple process?
Thanks
Mike
--- REVISED CODE ss1.php
Code: Select all
<?php
<html>
<body>
<?php
$form="<center><h2>Delivery etc - New Employee</h2></center>
<form method=GET action="ss2.php">
<b>Employee name:</b><br></br>
<input type="text" name="name" size=20>
<br></br><b>Department</b><br></br>
<select name="dept">
<option selected>Delivery Driver
<option>Warehouse
<option>Accounts
<option>Administration
</select>
<br></br>
<b>Pay Grade</b><br></br>
<select name="pay_grade">
<option selected>Top Job
<option>Normal
<option>Hope Employee Likes Overtime
<option>Does Not Even Register
</select>
<br></br>
<b>Country Location</b><br></br>
<select name="location">
<option selected>USA
<option>England
<option>Germany
</select>
<br></br>
<hr>
<input type="submit" value="Next Page">
<input type="reset" value="Clear">
</form>";
?>
</body>
</html>
<?php
echo $form;
?>
?>
--- REVISED CODE ss2.php
Code: Select all
<?php
<?php
# ss2.php
session_start();
# do any error checking on inputs here
$_SESSIONї'employee_no']=session_id();
$_SESSIONї'name']=$_GETї'name'];
$_SESSIONї'dept']=$_GETї'dept'];
$_SESSIONї'pay_grade']=$_GETї'pay_grade'];
$_SESSIONї'location']=$_GETї'location'];
?>
<html>
<body>
<?php
$form="<center><h2>Delivery etc - New Employee</h2></center>
<form method=GET action="ss3.php">
<b>Employee Addresss:</b><br></br>
<input type="text" name="address" size=30>
<br></br><br></br><b>Employee Telephone No:</b><br></br>
<input type="text" name="telephone" size=30>
<br></br>
<hr>
<input type="submit" value="Next Page">
<input type="reset" value="Clear">
</form>";
?>
</body>
</html>
<?php
echo "$form";
?>
?>
--- REVISED CODE ss3.php
Code: Select all
<?php
<?php
# ss3.php
session_start();
?>
<html>
<center><b><h3> Here are the new employee details</h3></b></center>
<?php
$display="<PRE>
Employee Name : $_SESSIONїname]<br></br>
Department : $_SESSIONїdept]<br></br>
Pay Grade : $_SESSIONїpay_grade]<br></br>
Location : $_SESSIONїlocation]<br></br>
Home Address : $_GETїaddress]<br></br>
Telephone : $_GETїtelephone]<br></br>
Employee ID : $_SESSIONїemployee_no]<br></br></pre>s";
?>
</body>
</html>
<?
echo "$display";
$to="admin@127.0.0.1";
$header="New Employee Addition";
$info= "Here are the details...
NAME: $_SESSIONїname]
DEPARTMENT: $_SESSIONїdept]
PAY GRADE: $_SESSIONїpay_grade]
HOME ADDRESS: $_GETїaddress]
TELEPHONE NO: $_GETїtelephone]
EMPLOYEE ID: $_SESSIONsїemployee_no]";
mail($to,$header,$info);
session_destroy();
?>
?>
Posted: Tue Oct 01, 2002 12:21 pm
by nielsene
Hmm I don't see anything that would cause that type of slow-down. Maybe your computer is a little slow to do the mail-ing and the server isn't pushing the html through to the client until the script ends.
I do wonder why you are storing the sessionid as an employee number. The sessionid generated by php will not be a consistent/good numbering scheme for use within the employee database. You should also make sure to always single or double quote the names of the variables in the GET/SESSION arrays. I noticed that you did most of the time, but missed it a few times. (If you develop with notice level error reporting on, PHP will alert you to these.)
Posted: Tue Oct 01, 2002 12:41 pm
by mikebr
I commented out the mailing and now ss3.php loads instantly.
I stored the session as the employee_id only because it does this in the book, it is only an exercise to learn php.
OK, I was a little confused with the quoting thing, I did a couple of scripts that seemed to have problems when I quoted the variables so I removed them when I was having these problems although at the end of the day they where probably nothing to do with the problem. I wasn't sure if they where to be used as single, double, when getting values with GET and maybe not with COOKIE but I think I am starting to understand quoting a little better now, the book I am using does not use them but from a couple of comments I have had from other users I now understand it is best to use them.
Thanks for your help on this.
Mike
Posted: Wed Oct 02, 2002 2:57 am
by twigletmac
You're probably having problems with quoting array element names and putting them into double quoted strings, this:
Code: Select all
$info= "Here are the details...
NAME: $_SESSIONїname]
DEPARTMENT: $_SESSIONїdept]
PAY GRADE: $_SESSIONїpay_grade]
HOME ADDRESS: $_GETїaddress]
TELEPHONE NO: $_GETїtelephone]
EMPLOYEE ID: $_SESSIONїemployee_no]";
should be
Code: Select all
$info= 'Here are the details...
NAME: '.$_SESSIONї'name'].'
DEPARTMENT: '.$_SESSIONї'dept'].'
PAY GRADE: '.$_SESSIONї'pay_grade'].'
HOME ADDRESS: '.$_GETї'address'].'
TELEPHONE NO: '.$_GETї'telephone'].'
EMPLOYEE ID: '.$_SESSIONї'employee_no'];
or
Code: Select all
$info= "Here are the details...
NAME: {$_SESSIONї'name']}
DEPARTMENT: {$_SESSIONї'dept']}
PAY GRADE: {$_SESSIONї'pay_grade']}
HOME ADDRESS: {$_GETї'address']}
TELEPHONE NO: {$_GETї'telephone']}
EMPLOYEE ID: {$_SESSIONї'employee_no']}";
Mac