Page 1 of 1
Redirect Script in PHP
Posted: Sun Apr 27, 2008 6:35 am
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.
Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 6:53 am
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

Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 5:13 pm
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>
Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 5:34 pm
by aceconcepts
You need an additional )
Replace:
with:
Code: Select all
if(isset($_POST['submit'])) //notice the additional )
{
Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 6:25 pm
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?
Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 6:39 pm
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
}
}
Re: Redirect Script in PHP
Posted: Sun Apr 27, 2008 10:05 pm
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());
?>