Page 1 of 1

SESSIONS and back and forth between pages

Posted: Thu Apr 02, 2009 2:11 am
by enoc22
Hi
I am a Newbie to php and I am running into a bit of a problem with sessions staying set when going to and from pages
i made a simple site with 3 pages (code bellow)
i enter text in isset.html and it is saved in the session['fred'] to isset1.php, it is also saved when go to isset2.php, but when i click to go back to isset1.php the session is blank. it is also blank if i click the "log out" button on isset1.php
any clues on how to get the session to stay, or should i use a cookie instead?
any help would be great!
Thanks in advance
Oliver

isset.html

Code: Select all

 
<html>
<body>
<form action="isset1.php" method="post">
    user name<input type="text" name="fred"/>
    <input type='submit' value='GO!!'/>
</form>
</body>
</html>
isset1.php

Code: Select all

<?
SESSION_START();
$_SESSION['fred'];
?>
<table width="100%" border="1">
<tr>
  <td colspan="3"><center><font size="+3" color="#5670F6">INKY</font></center></td>
</tr>
<tr>
  <td width="15%" >
  <a href="http://localhost/isset.html">ISSET.HTML</a><br>
  <a href="http://localhost/isset1.PHP">ISSET1.PHP</a><br>
  <a href="http://localhost/isset2.PHP">ISSET2.PHP</a>
  </td> 
  <td width="85%" >
        <? $_SESSION['fred']=$_POST['fred'];
        IF(ISSET($_SESSION['fred'])){
            echo"<h1> IT'S SET</h1>";
        }ELSE{
            echo"<h1> IT'S NOT SET</h1>";
        }
        echo $_SESSION['fred']; ?>
        <form action="isset1.php" method="post">
            <input type='submit' value='log out' name="log"/>
        </form>
    </td>
</tr>
</table>
isset2.php

Code: Select all

 
<?
SESSION_START();
$_SESSION['fred'];
?>
<table width="100%" border="1">
<tr>
  <td colspan="3"><center><font size="+3" color="#5670F6">INKY</font></center></td>
</tr>
<tr>
  <td width="15%" >
  <a href="http://localhost/isset.html">ISSET.HTML</a><br>
  <a href="http://localhost/isset1.PHP">ISSET1.PHP</a><br>
  <a href="http://localhost/isset2.PHP">ISSET2.PHP</a>
  </td> 
  <td width="85%" >
        <?echo $_SESSION['fred'] . "<br>";
        echo"fred<br>";?>
        </td>
</tr>
</table>

Re: SESSIONS and back and forth between pages

Posted: Thu Apr 02, 2009 2:18 am
by susrisha
The problem is in isset1.php

You are assigning the variable

Code: Select all

 
 $_SESSION['fred']=$_POST['fred'];
 
irrespective of whether the post value $_POST['fred'] is set or not

change the code like this at line 16 of the posted code of isset.php

Code: Select all

 
if(isset($_POST['fred']))
{
$_SESSION['fred'] = $_POST['fred'];
}
 

Re: SESSIONS and back and forth between pages

Posted: Thu Apr 02, 2009 2:38 am
by enoc22
Thanks for the reply, i changed the code on isset1.php to

Code: Select all

        IF(ISSET($_SESSION['fred'])){
            echo"<h1> IT'S SET</h1>";
        }ELSE{
            $_SESSION['fred']=$_POST['fred'];
            echo"<h1> IT'S NOT SET</h1>";
        }
the session is working i also added an unset button adn thats working
Thanks for the help