Connecting Tech Pros Worldwide Forums | Help | Site Map

sessions

Newbie
 
Join Date: Mar 2008
Posts: 5
#1: Mar 30 '08
hi everyone,

I have just started learning php. I have this problem with sessions.

There is a page called sesstest1.php where i can write my name and click the "click me" button. Now it goes to sesstest2.php where i want to display my name and a hyper link which takes me to the same page(sesstest2.php). The problem is if i am using $_SESSION=$_POST, then, only for the first time my name is getting displayed. How do be able to retrieve my name how many ever times i click on the hyper link.

Here is my code for sesstest1.php
[php] <?php
session_start();
?>
<html>
<head>
<title>
test page 1
</title>
</head>
<body>
<form name="test1" action="sesstest2.php" method="post">
<input type="text" name="username">
<input type="submit" name="submit" value="click me">
</form>
</body>
</html>[/php] this is my sesstest2.php
[php] <?php
session_start();
?>
<html>
<head>
<title>
test page 2
</title>
<head>
<body>
<?php
$_SESSION=$_POST;
echo $_SESSION['username'];
echo "<br/>";
$self=$SERVER['PHP_SELF'];
echo '<a href="'.$self.'">NEXT</a>';
?>
</body>
</html>[/php]Could you help? Thank you

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 30 '08

re: sessions


Welcome to The Scripts!

We are only prepared to help you when you adhere to the Posting Guidelines, in this particular case about enclosing all code within tags.

So please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Newbie
 
Join Date: Mar 2008
Posts: 5
#3: Mar 30 '08

re: sessions


Quote:

Originally Posted by ronverdonk

Welcome to The Scripts!

We are only prepared to help you when you adhere to the Posting Guidelines, in this particular case about enclosing all code within tags.

So please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR

sure, I will adhere to the rules in the future,
Thank you.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 30 '08

re: sessions


The $_SESSION array is overwritten each time the 2nd routine is invoked, even when there is nothing in the $_POST array, and that is when you issue the GET in the hotlink.

You must first test if there is anything submitted using POST and when so, save that in the $_POST array, like this starting at line 11 in the 2nd script[php]<?php
if (isset($_POST['username'])
$_SESSION['username']=$_POST['username'];
echo $_SESSION['username'];
echo "<br/>";
$self=$SERVER['PHP_SELF'];
echo '<a href="'.$self.'">NEXT</a>';
?>[/php]Ronald
Newbie
 
Join Date: Mar 2008
Posts: 5
#5: Mar 30 '08

re: sessions


Quote:

Originally Posted by ronverdonk

The $_SESSION array is overwritten each time the 2nd routine is invoked, even when there is nothing in the $_POST array, and that is when you issue the GET in the hotlink.

You must first test if there is anything submitted using POST and when so, save that in the $_POST array, like this starting at line 11 in the 2nd script[php]<?php
if (isset($_POST['username'])
$_SESSION['username']=$_POST['username'];
echo $_SESSION['username'];
echo "<br/>";
$self=$SERVER['PHP_SELF'];
echo '<a href="'.$self.'">NEXT</a>';
?>[/php]Ronald

Thank you very much, this solves my problem
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 30 '08

re: sessions


You are welcome. See you around some time.

Ronald
Reply