Jump to content

[SOLVED] Session Question?


Solarpitch

Recommended Posts

Hey,

 

I'm currently using this code to assign form details to session variables, then on the second piece of code I am trying to echo the value of the session variables but noting will print to screen. Here's an example of what I am trying..

 

// Get details from form and assign to session - then move to step 2

 


<?php

session_start();

$process = isset($_POST['process']) ? $_POST['process'] : '';

if ($process != "execute")
{
//not being submitted...
}
else
{	

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

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

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


header("Location:step2.php");

?>

}

 

 

 

// This is the code on the other page where I am trying to display the session values...

 







<?php

session_start();

echo $_SESSION['firstname'];
echo $_SESSION['lastname'];
echo $_SESSION['email'];

?>

Link to comment
Share on other sites

Judging from the appearance of the posted code, you have multiple newlines at the start of each file before the first <?php tag. These are content that are output to the browser and prevent the session from starting, on both pages.

 

New lines after the <?php tag are not content and are not output to the browser, unless specifically echoed using php code.

Link to comment
Share on other sites

There are 5 steps pages, each with different forms that gather different user info. I am storing all user details from the forms into a session, then on Step5.php I want to display all of the session details so the user can confirm the form.

 

Here's the form from Step1.php

 

teng84 - I tried that but still no joy

 



<form name="form1" method="post" action="step1.php">
                      <table width="563" border="0" cellspacing="2" cellpadding="0">
                        <tr>
                          <td> </td>
                          <td width="419"> </td>
                        </tr>
                        <tr>
                          <td width="138"><div align="right" class="style55">First name:</div></td>
                          <td><input name="firstname" type="text" id="firstname" style="width:200px; height:15px; " value="<?print $firstname; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Last name:</div></td>
                          <td><input name="lastname" type="text" id="lastname" style="width:200px; height:15px; " value="<?print $lastname; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Email address:</div></td>
                          <td><input name="email" type="text" id="email" style="width:200px; height:15px; " value="<?print $email; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Confirm email address </div></td>
                          <td><input name="cemail" type="text" id="cemail" style="width:200px; height:15px; " value="<?print $cemail; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Contact number:</div></td>
                          <td><input name="contact_no" type="text" id="contact_no" style="width:200px; height:15px; " value="<?print $contact_no; ?>"></td>
                        </tr>
                        <tr>
                          <td> </td>
                          <td> </td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Username</div></td>
                          <td><input name="username" type="text" id="username" style="width:200px; height:15px; " value="<?print $username; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Password:</div></td>
                          <td><input name="password" type="password" id="password" style="width:200px; height:15px; " value="<?print $password; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                        <tr>
                          <td><div align="right" class="style55">Confirm Password:</div></td>
                          <td><input name="cpassword" type="password" id="cpassword" style="width:200px; height:15px; " value="<?print $cpassword; ?>">
                              <span class="style69">*</span></td>
                        </tr>
                      </table>
                      <div align="right">
                        <input type="hidden" name="process" value="execute"> 
                        <input type="submit" name="Submit" value="Next >>">
                      </div>
                    </form>

Link to comment
Share on other sites

Judging from the appearance of the posted code, you have multiple newlines at the start of each file before the first <?php tag. These are content that are output to the browser and prevent the session from starting, on both pages.

 

New lines after the <?php tag are not content and are not output to the browser, unless specifically echoed using php code.

 

Thats only in the above example but this isnt the case on my page itself.

Link to comment
Share on other sites

try that if it works then it a header problam / whight spaces.........

 

<?php
ob_start();
session_start();
$process = isset($_POST['process']) ? $_POST['process'] : '';
if ($process != "execute"){
}else{	
$firstname = $_POST['firstname'];
$_SESSION['firstname'] = $firstname;
$lastname = $_POST['lastname'];
$_SESSION['lastname'] = $lastname;
$email = $_POST['email'];
$_SESSION['email'] = $email;
header("Location:step2.php");
ob_flush();
}
?>

 

 

<?php
ob_start();
session_start();
echo $_SESSION['firstname'];
echo $_SESSION['lastname'];
echo $_SESSION['email'];
ob_flush();
?>

Link to comment
Share on other sites

looks good to me but why did he have it not there no need for the ternary op || or good enough?

<?php
ob_start();
session_start();
$process =(isset($_POST['submit']) || $_POST['process'] ) ;
if ($process != "execute"){
}else{	
$firstname = $_POST['firstname'];
$_SESSION['firstname'] = $firstname;
$lastname = $_POST['lastname'];
$_SESSION['lastname'] = $lastname;
$email = $_POST['email'];
$_SESSION['email'] = $email;
header("Location:step2.php");
ob_flush();
}
?>

Link to comment
Share on other sites

Hey guys,

 

Just to let you know.. the session seemed to work when I used isset to check if the variable was set, then if it was assign the value to the session - and this works fine...

 


$firstname = $_POST['firstname'];

if(isset($firstname))

{$_SESSION['firstname'] = $firstname;}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.