Page 1 of 1

Session values lost upon refresh.

Posted: Mon Aug 03, 2009 1:09 am
by dissonantallure
Hello,


I have two forms the first is page1.php and the second is called page2.php. I have a session which I am trying to pass variables with. However, when I redirect to page2.php using

Code: Select all

header( 'Location: page2.php' ) ;
All variables are lost. Here is the code :


page1.php

Code: Select all

 
 
<?php 
session_start();    
 
$_SESSION['Name'] = $sender_name;   
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
</head>
 
<body>
 
 
<?php
 
// Define a variable for the email form input "Name"
$sender_name = $_POST['Name'];
 
 
 
 
// Create an empty array to hold the error messages.
$SC_Errors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
 
 
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
if ($sender_name=='Your Name')
// Define a variable for the error message
$SC_Errors['Name'] = ' Please Provide Your Name.';
 
 
// If the form input "Name" is only letters
if(preg_match("#^[A-Za-z' ]*$#",$sender_name)) {
$success;
}else {
$SC_Errors['Name'] = 'Invalid Name.';
} 
 
 
// If the error array is empty, there were no errors.
if (count($SC_Errors) == 0) {
 
header( 'Location: page2.php' ) ;
 
}
else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $scError = '<div class="formerror"><p><img src="alert.png" width="16" height="16" name="alert" id="alert" alt="alert" />Please review the form and correct the following errors:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($SC_Errors as $error) {
            $scError .= "<li>$error</li>";
        }
        $scError .= '</ul></div>';
    }
}
 
 
?>
 
 
<div align="center" class="SCerror">
<table> 
<tr>
<td>
<?php echo $scError; ?>
</td>
</tr>
</table>
</div>
 
 
<div align="center">
 
 
<form name="SCform" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
 
 
 
<h3>Your Information</h3>
<hr/>
 
<!-- Input: Name -->
<table> 
<tr>
<td>
 
<table>
 <tr>
  <td>
   <b>Name:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Name" id="Name" type="text"  style="color:#000000; position:static" onfocus="this.style.color = '#ccc' ; clickclear(this, 'Your Name');" onblur="this.style.color = '#000' ; clickrecall(this,'Your Name');" value="Your Name" />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Name'])) echo '<b> '.$SC_Errors['Name'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Name -->
 
 
<br />
 
 
 
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
 
</form>
      
</div>
 
</body>
</html>
 
 

and the second page called page2.php


Code: Select all

 
<?php 
session_start();    // This connects to the existing session
 
session_register ("Name");  // Create a session variable called name
 
$_SESSION['Name'] = $sender_name;   // Set name = form variable $sender_name
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
</head>
 
<body>
 
 
<?php
 
 
/* Gather Input Form Variables */
 
// Define a variable for the email form input "Name"
$sender_name = $_POST['Name'];
 
 
 
 
// Create an empty array to hold the error messages.
$SC_Errors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
 
 
// If the email form input "Name" is empty 
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
if ($sender_name=='Your Name')
// Define a variable for the error message
$SC_Errors['Name'] = ' Please Provide Your Name.';
 
 
// If the form input "Name" is only letters
if(preg_match("#^[A-Za-z' ]*$#",$sender_name)) {
$success;
}else {
$SC_Errors['Name'] = 'Invalid Name.';
} 
 
 
 
 
// If the error array is empty, there were no errors.
if (count($SC_Errors) == 0) {
 
header( 'Location: page2.php' ) ;
 
}
else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $scError = '<div class="formerror"><p><img src="alert.png" width="16" height="16" name="alert" id="alert" alt="alert" />Please review the form and correct the following errors:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($SC_Errors as $error) {
            $scError .= "<li>$error</li>";
        }
        $scError .= '</ul></div>';
    }
}
 
 
?>
 
 
<div align="center" class="SCerror">
<table> 
<tr>
<td>
<?php echo $scError; ?>
</td>
</tr>
</table>
</div>
 
 
 <div align="center">
 
 
<form name="SCform" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
 
 
 
<h3>Your Information</h3>
<hr/>
 
<!-- Input: Name -->
<table> 
<tr>
<td>
 
<table>
 <tr>
  <td>
   <b>Name:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Name" id="Name" type="text"  style="color:#000000; position:static" onfocus="this.style.color = '#ccc' ; clickclear(this, 'Your Name');" onblur="this.style.color = '#000' ; clickrecall(this,'Your Name');" value="Your Name" />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Name'])) echo '<b> '.$SC_Errors['Name'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Name -->
 
 
<br />
 
 
 
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
 
</form>
      
</div>
 
</body>
</html>
 

can anyone please help me?

Re: Session values lost upon refresh.

Posted: Mon Aug 03, 2009 8:05 am
by Eric!
So you are trying to do some simple error checking with page1.php, but page2.php is redefining the session variable data.

If the value is set in page1.php, then your code in page2.php is erasing it. Line 5 in page2.php sets it equal to nothing unless $Name is a global variable, which I doubt. Line 7 then sets your session variable equal to an undefined value. Then line 28 redefines it again!

Let's back up and show you how to access your session data properly, then you can figure out what you are trying to do with page2.php.

page2.php just needs Line 5 to be deleted. Line 7 needs to be
$sender_name=$_SESSION['Name'];
add an echo $sender_name; and you'll see the value

Ok, now you've passed the value via your session. Line 28 is going to try to stick another $_POST value into it, but there are a lot of problems with page2.php and I assume you haven't finished it because you are just trying to figure out your $_SESSION problem.

Re: Session values lost upon refresh.

Posted: Mon Aug 03, 2009 8:12 am
by jackpf
Try running var_dump($_SESSION) at various points throughout the scripts and see where they're lost.

Also, make sure you've got error reporting turned to full. You may have already sent out headers when you are trying to call session_start().

Re: Session values lost upon refresh.

Posted: Mon Aug 03, 2009 5:43 pm
by dissonantallure
Thank you guys for your help. I made the changes you suggested and now I am receving this error.
Warning: Cannot modify header information - headers already sent by (output started at /home/subatom1/public_html/Multi_Pages/page1.php:18) in /home/subatom1/public_html/Multi_Pages/page1.php on line 54

My updated code looks like this:


page1.php

Code: Select all

<?php
session_start();   
 
$_SESSION['Name'] = $sender_name;   
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
</head>
 
<body>
 
 
<?php
 
// Define a variable for the email form input "Name"
$sender_name = $_POST['Name'];
 
 
 
 
// Create an empty array to hold the error messages.
$SC_Errors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
 
 
// If the email form input "Name" is empty
if ($sender_name=='')
// Define a variable for the error message
$SC_Errors['Name'] = 'Enter Your Name.';
if ($sender_name=='Your Name')
// Define a variable for the error message
$SC_Errors['Name'] = ' Please Provide Your Name.';
 
 
// If the form input "Name" is only letters
if(preg_match("#^[A-Za-z' ]*$#",$sender_name)) {
$success;
}else {
$SC_Errors['Name'] = 'Invalid Name.';
}
 
 
// If the error array is empty, there were no errors.
if (count($SC_Errors) == 0) {
 
header( 'Location: page2.php' ) ;
 
}
else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $scError = '<div class="formerror"><p><img src="alert.png" width="16" height="16" name="alert" id="alert" alt="alert" />Please review the form and correct the following errors:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($SC_Errors as $error) {
            $scError .= "<li>$error</li>";
        }
        $scError .= '</ul></div>';
    }
}
 
 
?>
 
 
<div align="center" class="SCerror">
<table>
<tr>
<td>
<?php echo $scError; ?>
</td>
</tr>
</table>
</div>
 
 
<div align="center">
 
 
<form name="SCform" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
 
 
 <?php echo var_dump($_SESSION) ?>
 
 
 
<h3>Your Information</h3>
<hr/>
 
<!-- Input: Name -->
<table>
<tr>
<td>
 
<table>
 <tr>
  <td>
   <b>Name:</b>
  </td>
  <td>
  <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCinput"'; ?> align="center">
   <input name="Name" id="Name" type="text"  style="color:#000000; position:static" onfocus="this.style.color = '#ccc' ; clickclear(this, 'Your Name');" onblur="this.style.color = '#000' ; clickrecall(this,'Your Name');" value="Your Name" />
   </div>
  </td>
 </tr>
</table>
 
</td>
</tr>
<tr>
 <td>
 <div <?php if (!empty($SC_Errors['Name'])) echo ' class="SCerror"'; ?> align="center">
 <?php if (!empty($SC_Errors['Name'])) echo '<b> '.$SC_Errors['Name'].' </b>'; ?>
 </div>
 </td>
 </tr>
</table>
<!-- Input: Name -->
 
 
<br />
 
 
 
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
 
</form>
     
</div>
 
</body>
</html>

page2.php

Code: Select all

 
<?php
session_start();    // This connects to the existing session
 
 
$sender_name=$_SESSION['Name'];
 
 
// Define a variable for the email form input "Name"
$sender_name = $_POST['Name'];
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
</head>
 
<body>
 
 
<div align="center" class="SCerror">
<table>
<tr>
<td>
<?php echo $scError; ?>
</td>
</tr>
</table>
</div>
 
 
 <div align="center">
 
 
<form name="SCform" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
 
 
<h3>Your Information</h3>
<hr/>
<br />
 
 
 <?php echo $sender_name; ?>
 
 <?php echo var_dump($_SESSION) ?>
 
 
<br />
 
 
 
<input type="reset" value="Reset" name="Reset"> <input type="submit" name="Submit" value="Submit" />
 
</form>
     
</div>
 
</body>
</html>
 

I also tried
<?php echo var_dump($_SESSION) ?>
which gave me this output

Code: Select all

array(1) { ["Name"]=>  NULL }
My main objective is to take a fully functional php form I have created and break it into 3 parts. Which means I need to have the page check its self for errors and if there are any errors display the error message, and if there are no errors then store all variables and pass them to the next page. Thanks for all of your advice so far.

Re: Session values lost upon refresh.

Posted: Tue Aug 04, 2009 6:39 am
by jackpf
Right, that means that you're outputting something before you're calling session_start(). That includes any whitespace before your opening <?php tags.

You need to check all your files for any whitepsace, and any output before session_start().