php sessions problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oem
Forum Newbie
Posts: 1
Joined: Sun Nov 02, 2003 9:40 am

php sessions problem

Post by oem »

hello ... i´ve got a problem with sessions it seems that the session doesn´t register any help would be appreciated here´s the code for the three pages
Login Page:

Code: Select all

<?php
//checks if the form has been sent
if($_POST["postok"]) {
  //receives form data
  $nome = $_POST["nome"];
  $passwd = $_POST["passwd"];

 //records in the session if the fields aren´t blank
 if ( (!empty($nome)) && (!empty($passwd)) ) {
  //starts the session
  session_start();

  //registers session variables
  $_SESSION["sess_nome"] = $nome;
  $_SESSION["sess_passwd"] = $passwd;
 }//closes (empty)
 else {
  $erro++; //increments de error variable by one
  $html_error .= "<br><font face='Verdana' size='1' color='#5C718B'>Fields can´t be blank.</font>";
 }//closes else
}//closes IF($_post)
?> 


<html>
<head>
 <title>sessoes</title>
<link href="./css/main.css" rel="stylesheet" type="text/css">
</head> 

<body>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr> 
    <td><font size="1" face="Verdana"><strong>user authentication using sessions</strong></font></td>
 </tr>
 </table> 

 <hr noshade>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr bgcolor="#EBEDE4"> 
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; record session data &middot;</font></div></td>
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="verifica.php">check session</a> &middot;</font></div></td>
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="logout.php">logout</a> &middot;</font></div></td>
 </tr>
 </table> 

 <br>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td><font size="1" face="Verdana">the data willl be recorded in the session.</font></td>
 </tr>
 </table> 

 <br>
<?
 //checks if form has been posted
 if($_POST["postok"]) {
  //checks if error has occured
  if($erro) {
   echo "<div align='center'>$html_error</div>";
  }//closes if (error)
  // if there´s no error ...
  else {
  echo "<br><br><table border='0' cellpading='0' cellspacing='0' width='90%'>";
  echo "<tr>";
  echo "<td><div align='center'><font face='Verdana' size='1'><b>session started   !!</b></font></div></td>";
  echo "</tr>";
  echo "</table>";
 }//closes  ELSE(error)
} //closes IF(postok) 

//displays the form
else { ?>
 <form action="" method="post" name="frm_sessao">
  <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
   <td width="16%" height="25"><strong><font size="1" face="Verdana">Nome:</font></strong></td>
   <td width="84%" height="25"><font size="1" face="Verdana">
   <input name="nome" type="text" size="30" maxlength="30"></font></td>
  </tr> 

  <tr> 
      <td height="25"><strong><font size="1" face="Verdana">Password:</font></strong></td>
   <td height="25"><font size="1" face="Verdana">
   <input name="passwd" type="password" size="8" maxlength="8"></font></td>
  </tr> 

  <tr> 
   <td colspan="2"><div align="center"> 
   <input name="btnEnviar" type="submit" value="record session data" class="input-butt"></div></td>
   <input type="hidden" name="postok" value="1">
  </tr>
  </table>
 </form>
<?
} //closes ELSE?>
</body>
</html> 
?>
page verify:

Code: Select all

<?php

//starts the session
session_start();
session_register('sess_nome');
session_register('sess_passwd');
?> 

<html>
<head>
 <title>sessoes</title>
 <link href="./css/main.css" rel="stylesheet" type="text/css">
</head> 

<body>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr> 
  <td><font size="1" face="Verdana"><strong>user authentication using sessions</strong></font></td>
 </tr>
 </table> 

 <hr noshade>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr bgcolor="#EBEDE4"> 
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="admin.php">record session data</a> &middot;</font></div></td>
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; check session &middot;</font></div></td>
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="logout.php">logout</a> &middot;</font></div></td>
 </tr>
 </table> 

 <br><br>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td><font color="#FF0000" size="1" face="Verdana">results:</font></td>
 </tr>
 </table> 

 <br>
 <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td><font size="1" face="Verdana">
  <?
  //verifies if session variables are inicialized
  if(!empty($_SESSION["sess_nome"]) && (!empty($_SESSION["sess_passwd"])) ) {
    echo "<b>The session is active.</b><br><br>";
    echo "Name ...........: ".$_SESSION["sess_nome"];
    echo "<br>Passwd ............: ".$_SESSION["sess_passwd"];
  }//Closes IF 

  //If nothing is registered , display message
  else {
    echo "<font color='#5C718B'>unregistered session.</font>";
  } //closes else?></font></td>
 </tr>
</table>
</body>
</html> 
?>
logout page:

Code: Select all

<?php
<? 
 //starts the session
 session_start();

 //checks is the variables are registered
 if( (!empty($_SESSION["sess_nome"])) AND (!empty($_SESSION["sess_passwd"])) ) {
  //destroys the session
  $destroi = session_destroy();
 } //closes IF
?> 

<html>
<head>
 <title>sessoes</title>
 <link href="./css/main.css" rel="stylesheet" type="text/css">
</head> 

<body>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr> 
  <td><font size="1" face="verdana"><strong>user authentication using sessions !!! </strong></font></td>
 </tr>
 </table> 

 <hr noshade>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr bgcolor="#EBEDE4"> 
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="admin.php">record session data</a> &middot;</font></div></td>
  <td height="31"><div align="center"><font size="1" face="Verdana">&middot; <a href="verifica.php">check session</a> &middot;</font></div></td>
  <td height="31" bgcolor="#EBEDE4"><div align="center"><font size="1" face="Verdana">&middot; logout &middot;</font></div></td>
 </tr>
 </table> 

 <br><br>
 <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td><div align="center"><font size="1" face="Verdana">
 <?
 //checks session destroy
 if($destroi) {
  echo "<b>user successfully logout !!</b>";
 } 
 else {
  echo "<b>An error has occured. Please try again.</b><br><br>";
  echo "example: check if the session has been recorded.";
 }
?></font></div></td>
</tr>
</table>
</body>
</html> 
?>

Any help would be apprecciated
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

In your VERIFY page you have this at the top..

Code: Select all

session_register('sess_nome'); 
session_register('sess_passwd');
..which obviously registers the two session variables and that's ALL it does. The variables will be empty which means that this bit won't work...

Code: Select all

//verifies if session variables are inicialized 
  if(!empty($_SESSION["sess_nome"]) && (!empty($_SESSION["sess_passwd"])) ) {
..because you are checking to see if the variables ARE NOT empty.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[big_search]php sessions[/big_search]
Post Reply