Redirect Script in PHP

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
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Redirect Script in PHP

Post by latoyale »

Everah | Please use bbCode tags when posting code in the forums. It makes it a lot easier for people to read.
Good Morning,

I have the followng code in a form I am building. If certain things happen, the page redirects back to form. However I want the database to close when if a redirect happens. Right now, when someone puts in the wrong password it still writes to the database... wrong username it still writes to the database... etc.

How do I get it to stop writing to the database if it doesn't pass validation?

Here is part of the code:

Code: Select all

<?php
// Validation for password and answers
if($password != $password2){
echo "Your passwords do not match.  Please re-enter.<meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">";
 
}
elseif(strlen($_POST['password']) < 6){
echo "Password must contain at least 6 characters";
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
}
elseif($answer != $answer1){
echo "Your answer does not match. Please re-answer";
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
} 
//end validation
 
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database. Thank you for your registration.";
?>
Everah | Please use bbCode tags when posting code in the forums. It makes it a lot easier for people to read.
Last edited by RobertGonzalez on Sun Apr 27, 2008 6:33 pm, edited 1 time in total.
Reason: Added code tags
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Redirect Script in PHP

Post by aceconcepts »

Good morning,

What you could do is use an "error variable":

Code: Select all

 
$loginErr=0;
 
if(isset($_POST['submit'])
{
 
if($password != $password2){
   $loginErr=1;
}
elseif(strlen($_POST['password']) < 6){
   $loginErr=2;
}
elseif($answer != $answer1){
   $loginErr=3;
}
 
if($loginErr==0){
   //LOGIN OK
}
   else
{
   switch($loginErr)
   {
       case 1:
           echo"Passwords do not match!"; //redirect
       break;
 
       //etc...
   }
}
 
}
 
Using one error variable only allows you to output one error message. You could declare specific error variables in order to output multiple error message.

Hope it helps :D
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: Redirect Script in PHP

Post by latoyale »

When I put the script in my code, I received the following error:

Parse error: parse error, unexpected '{' in /home/content/j/a/b/jaberkjaber/html/askslicky/inner/signup_validation.php on line 28

My Code is:


<?php

$username = $_POST['username'];
$password = $_POST['password'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$password2 = $_POST['password2'];
$street = $_POST['street'];
$apt = $_POST['apt'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$birthday_month = $_POST['birthday_month'];
$birthday_day = $_POST['birthday_day'];
$birthday_year = $_POST['birthday_year'];
$gender = $_POST['gender'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$answer1 = $_POST['answer1'];


//end validation
$loginErr=0;

if(isset($_POST['submit'])
{

if($password != $password2){
$loginErr=1;
}
elseif(strlen($_POST['password']) < 6){
$loginErr=2;
}
elseif($answer != $answer1){
$loginErr=3;
}

if($loginErr==0){
//LOGIN OK
}
else
{
switch($loginErr)
{
case 1:
echo"Passwords do not match! <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
case 2:
echo"Passwords must contain at least 6 characters! <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
case 3:
echo"Your answer does not match. Please re-answer. <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
//etc...
}
}

}


$connect = mysql_connect('ip', 'database', 'password') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('askslicky') or die("Unable to Select Database<br>".mysql_error());

$query="INSERT INTO users (username, password,firstname, lastname, email, password2, street, apt, city, state, zip, phone, birthday_month, birthday_day, birthday_year, gender, question, answer, answer1) VALUES ('$username','$password','$firstname','$lastname','$email','$password2','$street','$apt','$city','$state','$zip','$phone','$birthday_month','$birthday_day', '$birthday_year','$gender','$question','$answer','$answer1')";

$success=mysql_query($query);

//username check

$name_check = "SELECT username FROM users WHERE username = '".$_POST['username']."'"; // check if username exists in database.


if (!($name_check)) print mysql_error();

$result = mysql_query($name_check);

if ($name_check == $username){
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
die('Sorry, the username: <strong>'.$_POST['username'].'</strong> is already taken, please pick another one.<meta http-equiv="Refresh"content="2;url=signup.php"/>')
;}


mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database. Thank you for your registration.";

?>

<html>
<head>
<meta http-equiv="Refresh"content="2;url=login_home.php"/>
<title></title>
</head>
<body>
</body>
</html>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Redirect Script in PHP

Post by aceconcepts »

You need an additional )

Replace:

Code: Select all

 
if(isset($_POST['submit'])
{
 
with:

Code: Select all

 
if(isset($_POST['submit'])) //notice the additional )
{
 
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: Redirect Script in PHP

Post by latoyale »

Thank you... that fixed that problem, however I have one more problem..

The break is still not working. No matter what I put in, the form still adds the info to the database and releases no error message. I even replaced the echo with print to see if I can get it to work.
My form has the following code in it:

<form action="signup_validation.php" method="post" name="signup_validation">

<div align="center">
<input name="submit" type="image" src="../images/btn-submit.gif" />
</div>
</form>

No matter what I do, I get:

" Your Information has been successfully added to the database. Thank you for your registration to Askslicky.com";

What am I doing wrong?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Redirect Script in PHP

Post by aceconcepts »

Before you execute your database caode you need to check whether there has been a previous error:

Put the following code within your starting if statement:

Code: Select all

 
if(isset($_POST['submit'])
{
   //validation code
 
if($loginErr==0)
{
   //database code here
}
 
}
 
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: Redirect Script in PHP

Post by latoyale »

I replaced the code and now I get blank output.... What can I do?

Code: Select all

<?php
 
$username = $_POST['username'];
$password = $_POST['password'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$password2 = $_POST['password2'];
$street = $_POST['street'];
$apt = $_POST['apt'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$birthday_month = $_POST['birthday_month'];
$birthday_day = $_POST['birthday_day'];
$birthday_year = $_POST['birthday_year'];
$gender = $_POST['gender'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$answer1 = $_POST['answer1'];
$submit = $_POST['submit'];
 
$connect = mysql_connect('ip', 'database', 'pass') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('database') or die("Unable to Select Database<br>".mysql_error());
//end validation
 
$loginErr=0;
 
if(isset($_POST['submit']))
{
 
if($password != $password2){
   $loginErr=1;
}
elseif(strlen($_POST['password']) < 6){
   $loginErr=2;
}
elseif($answer != $answer1){
   $loginErr=3;
}
 
if($loginErr==0){
 
$query="INSERT INTO users (username, password,firstname, lastname, email,  password2, street, apt, city, state, zip, phone, birthday_month, birthday_day, birthday_year, gender, question, answer, answer1) VALUES ('$username','$password','$firstname','$lastname','$email','$password2','$street','$apt','$city','$state','$zip','$phone','$birthday_month','$birthday_day', '$birthday_year','$gender','$question','$answer','$answer1')"; 
echo " Your Information has been successfully added to the database. Thank you for your registration.";
  
}
   else
{
   switch($loginErr)
   {
       case 1:
           print"Passwords do not match! "; //redirect
       break;
         case 2:
           print"Passwords must contain at least 6 characters! "; //redirect
       break;
        case 3:
          print"Your answer does not match.  Please re-answer. "; //redirect
       break;
              //etc...
   }
}
 
}
 
 
 
 
 
$success=mysql_query($query);
 
//username check
 
$name_check = "SELECT username FROM users WHERE username = '".$_POST['username']."'"; // check if username exists in database.
 
 
        if (!($name_check)) print mysql_error();
 
        $result = mysql_query($name_check);
    
        if ($name_check == $username){
        print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
        die('Sorry, the username: <strong>'.$_POST['username'].'</strong> is already taken, please pick another one.<meta http-equiv="Refresh"content="2;url=signup.php"/>')
        ;}          
 
                        
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
 
 
?>
Post Reply