Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MAINTAINING session values 2

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
How can one keep values obtained in a session, where the user/visitor hops fron page to page?. I don't want to use cookies, or put the values in the URL. Exampl of flow - A user arrives at a menu page, say Menu. They select a page of items to selcect, they select some and the values are kept. The user then returns to the Menu, goes off and selects other items. They either return and go through the process again, or go back to one of the pages and check their selections were correct or go to a send it page - mailto.

A senario - Menu has three pages to select from. Each page has 2 items to enter quantity against. How do I keep values during any permutation until reaching what could be called a checkout page. Regards
 
You use PHP Sessions. See
To get you started, in each script where you want to store or retrieve values, put
Code:
session_start();
before any output has occured. Then whenever you want to store a value, use
Code:
$_SESSION['some_meaning_str'] = $variable_name;
To use the value in another script, use
Code:
$new_variable = $_SESSION['some_meaning_str'];
//
// or just
//
echo $_SESSION['some_meaning_str'];

If you need anymore help, post your questions and any relavant code.

Ken
 
Thanks Ken. A bit more of what I am trying to do.

Menu.php has select buttons to go to pages where quantity items are entered agains
items. One such page would be as below.

Menu,php has the following line in the opening:

<?php
session_start();
?>

---------------------------------------------------------------

Page name - Entry1.HTML


<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
</head>

<body>
<form method="POST" action="Accept.php">
<br>


&nbsp;<table border="1" width="100%" id="table1">
<tr>
<td>DB6</td>
<td>6mins</td>
<td width="542">
<input type="text" name="DB6" size="12"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td width="542">&nbsp;</td>
</tr>
</table>
<input type="submit">
</form>
<p>


When submitted it goes to the next page below:

--------------------------------------------------------------
Page name Accept.php

<?php
session_start();

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

print '<html><body>
<a href="ShowEntry.php">click here</a>

</body></html>';
?><p>This is session1</p>



On clicking the button, it passes to the next page

--------------------------------------------------------------

Page name ShowEntry.php


<?php
session_start();
print 'The value of DB6 is: ' . $_SESSION['DB6'] . '
?>

<html>
<body>

<a href="Menu.php">click here</a>
</body>
</html>';


On clicking the button it returns to the Menu page.


---------------------------------------------------------------

PROBLEMS

The above sequence works in a straight line, I want it so that I go from the menu,
select page Entry1, select quantity, click button, return to the Menu.php.

Select Entry2,Entry3 etc, all returning back to the menu until a button "Finished" is
pressed where values can be seen. But if the user goes back to Menu.php, the figures
are still retained in the system.

Additionally, if a user goes back into an entry page, I want whatever previous selections
were made to still be there. Can I do that. Many thanks












 
I have sorted some of it out, referring to sleipner214's excellent help. The issue now seems to be - How can you preserve what entries are made my Entry forms. Basically, if someone enterd the page, fill out textboxes, leaves the form and goes back to double check. Hey presto, it's empty sunshine, you have to fill it again from start. Any easy way to overcome that?

Regards
 
Hi,
that's very easy..

Code:
<input type="text" name="name" value="<?=$_SESSION['name']?>" />

The same type of code for select's, etc.

Olav Alexander Mjelde
Admin & Webmaster
 
Many, Many thanks Olav. Works perfectly. Have a deserved star for that.

Just leaves last proble. How do I go from Page name - Entry1.HTML to Page name Accept.php (where it just puts in the values) to Menu.php. In other words I don't want to see the Accept page until the time I want to see it. I want to go from Menu/Entry/Menu/Entry2/Menu and then select to see values. Regards
 
Thank you for the star :)
I'm not sure I follow your next step..

Do you want to make something like:
step_1 -> step_2 -> step_3 ?

If so, you simply put inside the submit form:
<form action="page_1.php" method="post">
<form action="page_2.php" method="post">

etc. etc.

eg. submit to the next page.
If you wish to allow the session to be sent for those who can not accept session cookies, you have to also parse the SID.

you can parse it like so:
<form action="page_1.php?<?=SID?>" method="post">

if you wish to parse it via href:
<a href="page_2.php?<?=SID?>" title="visit page 2!!">page 2</a>

I Hope this was what you asked :p


Olav Alexander Mjelde
Admin & Webmaster
 
Thanks Olav.

Maybe I can make it a bit clearer.

Entry1.HTML - I want to enter quantities. Thats okay and works.

BUT - When I hit submit, I want to go straight back to the Menu1.php page.

I do not want to see the Accept.php page or the ShowEntry.php page. I cannot find how to make a silent/non visible version of Accept.php, or whether the passing of values into variables can be done in the Entry page during submit.

The ShowEntry.php page is only to be called when needed.

Hope this is making sense. Regards






Then I want to automatically return to the menu, passing through the Accept.php page where values are put into variables - $_SESSION['DB6'] = $_POST['DB6'];

<?php
session_start();

$_SESSION['DB6'] = $_POST['DB6'];
 
Sorry, cut and pasted above should not show lines at bottom - "Then I want to automatically return" etc, etc
 
Hi,
You can either use header with location to do this, meta-refresh (client dependent) OR you can make a function to handle your actions, which you can include in the menu file.

header location with 301 moved, would work, I think.

eg:
Code:
// your code here (NOTHING PARSED THOUGH!!)
header("HTTP/1.1 301 Moved Permanently"); // to avoid google penalizing you
header("location: [URL unfurl="true"]http://www.domain.tld/");[/URL] // where you want to go!
header("Connection: close");  // fix for IE bug

Olav Alexander Mjelde
Admin & Webmaster
 
Many thanks to all those that struggled to help me during my fraught entrance into PHP. I have just found after reading a book, (not quite all of it) that my server has not got trans-sid fully implemeted, therefore its a cookie issue why (header ....) is not working.

Many thanks.
 
yes, but the sample code I gave you above, solves that issue.

you have to transfer the SID to the next page, in the querystring.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks Olav,

I tried this, but the values do not get passed when cookies rejected in the browser. The server administrators say they have not compiled PHP on the server wit trans-sid active, so any other thoughts? or have I put the code in below incorrectly. Thanks

<?php
session_start();
$_SESSION['DB6'] = $_POST['DB6'];
$_SESSION['DB12'] = $_POST['DB12'];
$_SESSION['DB22'] = $_POST['DB22'];

// your code here (NOTHING PARSED THOUGH!!)
header("HTTP/1.1 301 Moved Permanently"); // to avoid google penalizing you
header("Location: Menu1.php"); // where you want to go!
header("Connection: close"); // fix for IE bug
?>
 
I'd bet the problem is that your use of the header('Location: ...') is working counter to PHP's trans-sid system.

PHP will, if given the opportunity, automagically tweak tags to make sure the session ids get through. If you're moving the browser around using Location HTTP headers, the trans-sid system probably can't tweak the URL.

I recommend that you do it by hand. Fetch the session id (see session_id()) then explicitly include that ID in the URL you use with header ('Location:...');

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir,

I tried putting this code into the Menu page, the first page that opens to see what would happen, hoping to see a session ID. But nothing appears. I think I will just have leave it to there, needing cookies enabled, as its taking so long to get just the basics together.

At least I can get something out, even if its not what I wanted. Regards.

<?php
session_start();
$ses_id = session_id();
echo "id=".$_SESSION['$ses_id'];
?>


 
Just read, session id to be asked for before session start.

<?php
$ses_id = session_id();
echo "id=".$_SESSION['$ses_id'];
session_start();
?>

Got a little unhappy, warning message that headers already gone out, although HTML follows after code above. Not to worry, going to bury it now. Regards
 
Sorry sleipnir,

I went back to the link, and found the example, saw session id to be asked for before session start. Thanks for your message just behind mine. Now that I have an ID, can you tell me where it is put in the syntax:

header("Location: Menu1.php");

Many thanks.
 
Play around with trans-sid. See what PHP puts in links, etc, to pass the session ID around. Duplicate that in the URL to which your Location: header points.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir,

I don't know why but your code bombs out. I looked in the PHP manual under regenerate, modified as below, and that works, and against what is said, session_start last?

<?php
session_start();
$ID_sessionid = session_id();
echo "SessionID=: $ID_sessionid<br />";
?>

Ref trans-sid. The hosting company with the server say they do not intend to recompile the server with trans-sid on. If they did it for me it would affect all their clients!

I am reading an article on putting the session ID into a hidden field rather than the URL. Trying to make sense of that, but least I can SEE an ID. Thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top