0

Hi,

I have created a mock site for a shop which has a registration form which is validated and then, if all the items pass the validation it should move on to the next page. The validation of the individual form feilds work well, I just can't get the redirecting bit to work properly, it either redirects regardless of the conditions of the if statement that controls it or it won't redirect at all. I have tried all methods including using the number of expected valid items as a condition (eg if $validated < 7) or using trying to change the value of $validated depending on the success/failure of the validation process and then using if $validated != 'false' and loads of other methods and I just can't get it to work. It's now driving me nuts.

I've got a feeling that it's something very silly and because I've been looking at it for so long I can't see it. I'd be really grateful if someone could look over my code and tell me where I've gone wrong so I can get on with my life!

tia Tatty

<?php
session_start();

	if (isset($_POST['log_out'])){
		$_SESSION['is_logged_in'] ="no";
		session_unset();
		session_destroy();
		
header("Location:index.php");
exit();
	}
//Connect to server
$conn = mysql_connect ("localhost", "username", "password") or die ("Could not connect to database.");
//connect to database
mysql_select_db("tatty27_iwtgadget") or die ("Database does not exist.");
?>
<?
if(isset($_POST['register'])){
		if ($validated == 'true'){
				$_SESSION['firstName'] = $firstName;
				$_SESSION['surname'] = $surname;
				$_SESSION['salutation'] = $salutation;
				$_SESSION['address1'] = $address1;
				$_SESSION['address2'] = $address2;
				$_SESSION['city'] = $city;
				$_SESSION['county'] = $county;
				$_SESSION['country'] = $country;
				$_SESSION['postcode'] = $postcode;
				$_SESSION['pass'] = $pass;
				$_SESSION['DOB'] = $DOB;
				$_SESSION['email'] = $email;
				$_SESSION['subscribeMail'] = $subscribeMail;
				$_SESSION['bday'] = $bday;
				$_SESSION['dateReg'] = $dateReg;
				$_SESSION['day'] = $day;
				$_SESSION['month'] = $month;
				$_SESSION['year'] = $year;
				$DOB = $year.'-'.$month.'-'.$day;
				$dateReg = date('Y-m-d'); 
				$subscribeMail = $_POST['subscribeMail'];				
header("Location:successful_login.php");
exit();
			}
}

$monthName = array("January","February","March","April","May","June","July","August","Spetember","October","November","December");	
	function required()
	{
		echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000" > Required field</font>';
	}

function checkPostcode (&$toCheck) {

  // Permitted letters depend upon their position in the postcode.
  $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1
  $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2
  $alpha3 = "[abcdefghjkstuw]";                                   // Character 3
  $alpha4 = "[abehmnprvwxy]";                                     // Character 4
  $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5
  
  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';

  // Expression for postcodes: ANA NAA
  $pcexp[1] =  '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';

  // Expression for postcodes: AANA NAA
  $pcexp[2] =  '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
  
  // Exception for the special postcode GIR 0AA
  $pcexp[3] =  '^(gir)(0aa)$';
  
  // Standard BFPO numbers
  $pcexp[4] = '^(bfpo)([0-9]{1,4})$';
  
  // c/o BFPO numbers
  $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';

  // Load up the string to check, converting into lowercase and removing spaces
  $postcode = strtolower($toCheck);
  $postcode = str_replace (' ', '', $postcode);

  // Assume we are not going to find a valid postcode
  $valid = false;
  
  // Check the string against the six types of postcodes
  foreach ($pcexp as $regexp) {
  
    if (ereg($regexp,$postcode, $matches)) {
      
      // Load new postcode back into the form element  
      $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
      
      // Take account of the special BFPO c/o format
      $toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
      
      // Remember that we have found that the code is valid and break from loop
      $valid = true;
      break;
    }
  }

  if ($valid){return true;} else {return false;};
}

?>
 
<!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" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>I Want That Gadget - Register</title>

<link href="elements.css" rel="stylesheet" type="text/css" />
<link href="images.css" rel="stylesheet" type="text/css" />
<link href="text.css" rel="stylesheet" type="text/css" />
<link href="forms.css" rel="stylesheet" type="text/css" />
<link href="tables.css" rel="stylesheet" type="text/css" />
<link href="links.css" rel="stylesheet" type="text/css" />


<style type="text/css">



label
{
width: 85px;
float: left;
text-align: left;
margin-left:25px;
margin-right: 5px;
display: block;
color:#FFF;

}

a:link { 
	color:#FC6;
	}
a:visited { 
	color:#FC6;
	}
	
#search_results {
	position:absolute;
	top:200px;
	font-family: sans-serif;
}
form#register{
	position:absolute;
	top:86px;
	color:#000;
	text-align:left;
	margin: 0 auto;
	font-family: sans-serif;
}

.left_menu:hover {
	text-decoration: none;
	color: #D4340C;
}

<!--[if ie 6]><link rel="stylesheet" type="text/css" media="screen" href="ie6.css"-->
</style>
</head>

<body>
<div id="container">
<?php include ("top_section.php"); ?>
<div id="mid_nav_bar">
    <ul class="glossymenu">
      <li><a href="index.php"><b>Home</b></a></li>
      <li><a href="contact_us.php"><b>Contact Us</b></a></li>
      <li><a href="search_results.php"><b>Search Results</b></a></li>
      <li><a href="facts.php"><b>Random Facts</b></a></li>
      <li><a href="cart.php"><b>View Cart</b></a></li>	
      <li><a href="jokes.php"><b>Jokes</b></a></li>
      <li><a href="wish_list.php"><b>Wish List</b></a></li>
      <?
	if ($_SESSION['accountType'] == 'admin'){
    echo '<li>'.'<a href="admin_portal.php">'.'<b>'.'Admin Portal'.'</b>'.'</a>'.'</li>';
	}else{
		echo '<li class="current">'.'<a href="account.php">'.'<b>'.'Your Account'.'</b>'.'</a>'.'</li>';
	}
	?>
  </ul>
  </div> 
  <div id="lower_half">
<?php include ("left_menu.php"); ?>

    	<div id="center">
  <?php include ("random_search.php");?> 
 <!-- centre panel-->
 <form id="register" action="" method="post">
<table border="0">
  <tr>
    <td width="175">Title:</td>
    <td colspan="4"><select  name="salutation" maxlength="60"/>
    
      <option></option>
      <option>Mr</option>
      <option>Mrs</option>
      <option>Miss</option>
      <option>Dr</option>
      <option>Rev</option>
    </td>
<?php 

	$salutation = $_POST['salutation'];

?>
  </tr>
  <tr>
    <td>First Name:</td>
    <td colspan="3"><input type="text" name="firstName" maxlength="60" style="width:225px" value="<?php echo $_POST['firstName'];?>"/>
      <font color="#CC0000">*</font>
      <?php 
			if($_POST['firstName'] == ''){
				required();
				$validated = 'false';
	 		 }else
			 		{
			 			$firstName = $_POST['firstName'];
						$validated = 'true';
					}
?>    </td>
    </tr>
  <tr>
    <td>Surname:</td>
    <td colspan="3"><input type="text" name="surname" maxlength="60" style="width:225px" value="<?php echo $_POST['surname'];?>"/>
      <font color="#CC0000">*</font>
      <?php 
			if($_POST['surname'] == ''){
				required();
				$validated = 'false';
	 		 }else
			 	   {
			 		  $surname = $_POST['surname'];
					  $validated = 'true';
			       }
?>    </td>
    </tr>
  <tr>
    <td>Address 1:</td>
    <td colspan="3"><input type="text" name="address1" maxlength="60" style="width:225px" value="<?php echo $_POST['address1']; ?>"/>
      <font color="#CC0000">*</font>
      <?php 
			if($_POST['address1'] == ''){
				required();
				$validated = 'false';
	 		 }else
			 		{
					 	$address1 = $_POST['address1'];
						$validated = 'true';
					}
?>    </td>
    </tr>
  <tr>
    <td>Address 2:</td>
    <td colspan="2"><input type="text" name="address2" maxlength="60" style="width:225px" value="<?php echo $_POST['address2']; ?>"/>
    </td>
    <td width="85">
  <?php 
			if($_POST['address2'] == '')
					{
						$address2 = $_POST['address2'];
					}
?>
    </td>
  </tr>
  <tr>
    <td>City/Town:</td>
    <td colspan="3"><input type="text" name="city" maxlength="60"  style="width:225px" value="<?php echo $_POST['city']; ?>"/>
      <font color="#CC0000">*</font>
      <?php
			if($_POST['city'] == ''){
				required();
				$validated = 'false';
	  		}else	{
						$_SESSION['city'] = $_POST['city'];
						$validated = 'true';
					}
?>    </td>
    </tr>
  <tr>
    <td>Postcode:</td>
    <td colspan="3"><input type="text" name="postcode" maxlength="60" style="width:225px" value="<?php echo $_POST['postcode']; ?>"/>
      <font color="#CC0000">*</font>
      <?php
			if($_POST['postcode'] == ''){
				required();
				$validated = 'false';
	  		}else{
				if (!checkpostcode($_POST['postcode'])){
					echo '<font size="-1.5" color="#CC0000" >Invalid Postcode</font>';
				}else
					{
						$postcode = $_POST['postcode'];
						$validated = 'true';
					}
			}
?>    </td>
    </tr>
  <tr>
    <td>County:</td>
    <td colspan="2"><input type="text" name="county" maxlength="60" style="width:225px" value="<?php echo $_POST['county']; ?>"/></td>
<?    
						$county = $_POST['county'];
?>
  </tr>
  <tr>
    <td>Country:</td>
		<?php include("country.php"); ?>
<? 
						$country = $_POST['country'];
?>
  </tr>
  <tr>
    <td>Date of Birth DD-MM-YYYY</td></td>
<td colspan="3"><input type="text" size="2" name="day" maxlength="2" value="<?php echo $_POST['day']; ?> " onFocus="clearDefault(this)"/>
  <input type="text" size="2" name="month" maxlength="2" value="<?php echo $_POST['month']; ?>" onFocus="clearDefault(this)"/>
  <input type="text" size="4" name="year" maxlength="4" value="<?php echo $_POST['year']; ?>" onFocus="clearDefault(this)"/>
  <font color="#CC0000">*</font>      
  <?php
 
	 if(($_POST['day']=='')||($_POST['month']=='')||($_POST['year']=='')){
						required();
						$validated = 'false';
	 }else{
	 $monthName = array('01' => 'January','02' => 'February','03' => 'March','04' => 'April','05' => 'May','06' =>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December');
	 $day = $_POST['day'];
	 $month = $_POST['month'];
	 str_pad($month, 2, "0", STR_PAD_LEFT); 
	 $year = $_POST['year'];
	 
	  if ($month == '02'){
		 if($day < 1 || $day > 29){
			 echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000">There are not that many days in February!</font>';
			 $validated = 'false';
		 }
	 }
		 else if(($month == '04') || ($month == '06') || ($month == '09') || ($month == '11')){
					if ($day < 1 || $day > 30){
						echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000">There are not that many days in '.$monthName["$month"].'!</font>';
						$validated = 'false';
						
					}
		 }
					else if ($day < 1 || $day > 31){
						echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000">There are not that many days in that month!'.'</font>';
						$validated = 'false';
					}
					else $_SESSION['birth_ok'] =='ok';
					$validated = 'true';
		 }
/* }*/
?></td>
  </tr>
  <tr>
    <td nowrap="nowrap">Password:<br />
      <font size="-2">(min 4, max 8 charcters)</font></td>
    <td colspan="3"><input type="password" name="pass" maxlength="10" style="width:150px" value="<? echo $_POST['pass'];?>" onFocus="clearDefault(this)"/>
      <font color="#CC0000">* </font>
<?php
			if($_POST['pass'] == ''){
				required();
				$validated = 'false';
	  		}else
				
							{
								$pass = $_POST['pass'];
								$validated = 'true';
							}
?>
      </td>
     
    </tr>
  <tr>
    <td>Confirm Password:</td>
    <td colspan="3"><input type="password" name="pass2" maxlength="10" style="width:150px" value="<? echo $_POST['pass2']; ?>" onFocus="clearDefault(this)"/>
      <font color="#CC0000">*</font>
      <?php 
			 	$pass2 = $_POST['pass2'];
				
			 if(((strlen($pass) <= 3) || (strlen($pass) >= 9))){
                                echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000">password has an incorrect number of characters</font>';
								$validated = 'false';
                        }
						else if($pass != $pass2){
				echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000">passwords don\'t match</font>';
				$validated = 'false';
			}
			else $_SESSION['password'] =='ok';
			$validated = 'true';
			
?>
     </td>
    </tr>
  <tr>
    <td>Email Address:</td>
    <td colspan="3"><input type="text" name="email" style="width:225px" value="<?php echo $_POST['email']; ?>"/>
      <font color="#CC0000">*</font>
      <?php 		
			$email = $_POST['email'];
			$email_check = mysql_num_rows(mysql_query("SELECT * FROM customer WHERE email = '$email'"));
			
			if($_POST['email'] == ''){
				required();
				$validated = 'false';
	  		}else if ($email_check > 0){
				echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000" >This email address is already registered</font>';
				$validated = 'false';
						}else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
		echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000" >Invalid email address</font>';
		$validated = 'false';
						}else						
								{
									$_SESSION['email'] == 'ok';
									$validated = 'true';
								}
	
?>    </td>
    </tr>
  <tr>
    <td>Would you like to receive<br />
      offers by email?:</td>
    <td colspan="2"><input type="checkbox" name="subscribeMail" maxlength="60" font /><? echo $validated; ?></td>
    <?
		if($_POST['subscribeMail']!=''){
			$subscribeMail == 'yes';
			$_SESSION['subscribeMail'] == 'yes';
		}	
	?>
   
  </tr>
  <tr>
    <td>Terms and Conditions:</td>
    <td colspan="2"><textarea name="terms" style="width:225px; height:60px; text-align:justify">
This website has been constructed for a university project and nothing on this site is actually available to purchase and the prices and information about the products are totally fictitious.  Should you happen to fill this form in and submit the details the infomration will be stored within a database and by checking the 'I agree' option you therefore agree to your details being used within the context of this website and project alone.  Your details will not be shared with any other organisation other than Burton-on-Trent College and Staffordshire University when the project is both demonstrated to them and examined by them.</textarea></td>
  </tr>
  <tr>
    <td>I agree to terms:</td>
    <td colspan="4"><input type="checkbox" name="terms" maxlength="60" />
      <font color="#CC0000">*</font>
      <?
			if($_POST['terms'] !="on"){
				echo '<img src="/images/warning.gif" width="18" height="18" alt=""><font size="-1.5" color="#CC0000" >You must agree to the terms continue</font>';
				$validated = 'false';
				
				}else
			 		{
					 	$validated = 'true';
						
					}
					
	
		
	?>
        </td>
      
  </tr>
  <tr>
    <td><font color="#CC0000">* Required field</font></td>
    
    
    <td colspan="3"><input type="submit" name="register" value="Register" style="text-align:right" /></td>
  </tr>
</table>
</form>

    	</div>  
	</div>  
</div>

</body>

</html>
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by tatt727
0

You are mixing $_POST and session variables. You shouldn't test if ($_POST[... but if use the session vaiables. Like so:

<?php
// Start the session...
session_start();

// If the form's been submitted
if (isset($_POST['submit'])){
   // Put the values into session variables
   foreach ($_POST as $key => $value){
      if ($key != "submit"){
         $_SESSION[$key] = $value;
      }
   }
   // Validate the data
   if ($validated == "yes"){
      if ($_SESSION['username'] == 'Admin'){
          header("Location: page2.php");
      }
      else{
          header("Location: page3.php");
      }
}

// Echo the form
echo <<<FORM
<form name="register" action="{$_SERVER['PHP_SELF']}" method="post">
 <input type="text" name="firstname" value="{$_SESSION['firstname']}">First Name<br>
 <input type="text" name="lastname" value="{$_SESSION['lastname']}">Last Name<br>
 <input type="text" name="username" value="{$_SESSION['username']}">Username<br>
 <input type="submit" name="submit" value="submit">
</form>
FORM;
?>

Edited by colweb: n/a

0

I'm afraid it's doing the same, I've changed to code to

<?php
session_start();

	if (isset($_POST['log_out'])){
		$_SESSION['is_logged_in'] ="no";
		session_unset();
		session_destroy();
		$validated = 'no';		
header("Location:index.php");
exit();
	}
//Connect to server
$conn = mysql_connect ("localhost", "username", "password") or die ("Could not connect to database.");
//connect to database
mysql_select_db("tatty27_iwtgadget") or die ("Database does not exist.");
?>
<?
if(isset($_POST['register'])){
 		 foreach ($_POST as $key => $value){

    			if ($key != "register"){
 
					$_SESSION[$key] = $value;

				}

		}
				
}
if ($validated = 'yes'){
header("Location:successful_login.php");
exit();
			}



  


?>
 
<!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" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>I Want That Gadget - Register</title>

<link href="elements.css" rel="stylesheet" type="text/css" />
<link href="images.css" rel="stylesheet" type="text/css" />
<link href="text.css" rel="stylesheet" type="text/css" />
<link href="forms.css" rel="stylesheet" type="text/css" />
<link href="tables.css" rel="stylesheet" type="text/css" />
<link href="links.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div id="container">
<?php include ("top_section.php"); ?>
<div id="mid_nav_bar">
    <ul class="glossymenu">
      <li><a href="index.php"><b>Home</b></a></li>
      <li><a href="contact_us.php"><b>Contact Us</b></a></li>
      <li><a href="search_results.php"><b>Search Results</b></a></li>
      <li><a href="facts.php"><b>Random Facts</b></a></li>
      <li><a href="cart.php"><b>View Cart</b></a></li>	
      <li><a href="jokes.php"><b>Jokes</b></a></li>
      <li><a href="wish_list.php"><b>Wish List</b></a></li>
      <?
	if ($_SESSION['accountType'] == 'admin'){
    echo '<li>'.'<a href="admin_portal.php">'.'<b>'.'Admin Portal'.'</b>'.'</a>'.'</li>';
	}else{
		echo '<li class="current">'.'<a href="account.php">'.'<b>'.'Your Account'.'</b>'.'</a>'.'</li>';
	}
	?>
  </ul>
  </div> 
  <div id="lower_half">
<?php include ("left_menu.php"); ?>

    	<div id="center">
  <?php include ("random_search.php");?> 
 <!-- centre panel-->
 <form id="register" action="" method="post">
<table border="0">
  <tr>
    <td width="175">Title:</td>
    <td colspan="4"><select  name="salutation" maxlength="60"/>
    
      <option></option>
      <option>Mr</option>
      <option>Mrs</option>
      <option>Miss</option>
      <option>Dr</option>
      <option>Rev</option>
    </td>
<?php 

	$salutation = $_POST['salutation'];

?>
  </tr>
  <tr>
    <td>First Name:</td>
    <td colspan="3"><input type="text" name="firstName" maxlength="60" style="width:225px" value="<?php echo $_POST['firstName'];?>"/>
      <font color="#CC0000">*</font>
      <?php 
			if($_POST['firstName'] == ''){
				required();
				$validated = 'no';
	 		 }else
			 		{
			 			$firstName = $_POST['firstName'];
						$validated = 'yes';
					}
?>    </td>
    </tr>
  <tr>

rest of table to submit...

Edited by tatt727: n/a

0

I see you are still using $_POST but should use $_SESSION. The only $_POST you can test for is the submit type, for all others forget $_POST but use $_SESSION.

0

Hi,

Thanks for that, I misunderstood what you had meant the first time round.

Thanks for your help, it is grately appreciated

Tatty

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.