Maintaining state seems to be a problem - hmmmm???

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
benok
Forum Newbie
Posts: 1
Joined: Wed Jul 27, 2005 6:58 am

Maintaining state seems to be a problem - hmmmm???

Post by benok »

Hi everyone,

I have a signup.php file which contains a whole bunch of fields that users put details into when they are registering for my website. When the click the submit button, it goes to validate.php to validate all the fields are valid (i.e -> not empty etc etc).

So when the user clicks submit it goes to validate.php, I get all the variables from the first page by doing something like this.

$_SESSION['phpFirstName'] = $_POST['htmlFirstName'];
etc
etc

If there is an error, I want to go back to the signup.php page and notify the user of the offending fields on the form. Say the user didn't fill in the first name, I would return to the signup page like this:

header("location:http://address/signup.php?errors=firstname");

when I go back to the signup.php page, I expect that all other variables that passed validation have been "session"ized and that I can display them in the form without the user having to fill them in again - however the session variable doesn't seem to work and there is nothing in there when i return.

How can this be???
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Are you accepting the session cookie? or are you rejecting it?
beno747
Forum Newbie
Posts: 4
Joined: Thu Jun 16, 2005 12:17 am

Post by beno747 »

I don't know whether I'm accepting a session cookie or not.....how do I accept a session cookie???
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Post code or we can't help

Remember you need session_start() on every page.
beno747
Forum Newbie
Posts: 4
Joined: Thu Jun 16, 2005 12:17 am

Post by beno747 »

Hi jshpro2,

Here is a snippet of signin.php

<form name="frmSignup" method="post" action="validate.php">
<input type="hidden" name="blnFirstTime" value="False">

<p><strong><font color="#CC00CC">Personal Details</font></strong></p>
<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="24">Please enter a username</td>
<td><input type="text" name="txtboxUsername" value="<?php print($_SESSION['strUsername']) ?>"></td>
</tr>
<tr>
<td height="24">Please enter a password <font size="2"><em>(must be at least
6 characters long)</em></font></td>
<td><input type="password" name="textfield18"></td>
</tr>
<tr>
<td height="24">Retype your password</td>
<td><input type="password" name="textfield19"></td>
</tr>
<tr>
<td width="349" height="24">First Name</td>
<td width="356"><input type="text" name="txtboxFirstName" value="<?php print($_SESSION['strFirstName']) ?>"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtboxLastName" value="<?php print($_SESSION['strLastName']) ?>"></td>
</tr>
<tr>


In validate.php, I have

<?php
$errors = "";

if(empty($_POST['txtboxUsername']))
$errors = $errors . "username" . "+";
else
$_SESSION['strUsername'] = $_POST['txtboxUsername'];

and so on.......

down the bottom I have

if($errors <> "")
header("location:http://localhost:81/clubsuntory/signup.php?errors=" . $errors);

else
header("location:http://localhost:81/clubsuntory/signup.php");
?>


Thanks a lot for helping out.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You must put session_start() at the top of both pages for it to work.
beno747
Forum Newbie
Posts: 4
Joined: Thu Jun 16, 2005 12:17 am

Post by beno747 »

what about if I set session.auto_start = 1 in my php.ini file. Will I then have to call session_start() explicitly??


Thanks again mate
Post Reply