Jump to content

Execution of statements


dotkpay

Recommended Posts

I have been writing php scripts for a short while and I just encountered something unusual. :confused: The php statements in particular files, notably the conditional statements are executed upside down.

As in the statements at the bottom of the page are parsed before some in the middle of the script most especially when it comes to the 'if' statements. Some 'if' statements are skipped even when the condition is true and then the 'elseif' statements are executed.

What could be the problem? :-[

:DThanks in advance. ;) ;)

Link to comment
Share on other sites

This is the code. It is actually a script that conducts user registration by picking data posted from a form. What puzzles me is that the last statement of redirecting to 'complete.php' can get executed before anything else and therefore rendering much of the script useless.

 

 

<?php

require("connect.php");

 

$first = "{$_POST['first']}";

$last = "{$_POST['last']}";

$email = "{$_POST['email']}";

$phone = "{$_POST['phone']}";

$username = "{$_POST['username']}";

$password = "{$_POST['password']}";

$password2 = "{$_POST['password2']}";

 

// Disarm user entries

 

$first = stripslashes($first);

$last = stripslashes($last);

$email = stripslashes($email);

$phone = stripslashes($phone);

$username = stripslashes($username);

$password = stripslashes($password);

$password2 = stripslashes($password2);

$first = mysql_real_escape_string($first);

$last = mysql_real_escape_string($last);

$email = mysql_real_escape_string($email);

$phone = mysql_real_escape_string($phone);

$username = mysql_real_escape_string($username);

$password = mysql_real_escape_string($password);

$password2 = mysql_real_escape_string($password2);

 

 

// If any field is NULL

 

if ($first==NULL)

{

header("location:first.php");

}

elseif ($last==NULL)

{

header("location:last.php");

}

elseif ($email==NULL)

{

header("location:email.php");

}

elseif ($phone==NULL)

{

header("location:phone.php");

}

elseif ($username==NULL)

{

header("location:username.php");

}

elseif ($password==NULL)

{

header("location:nopassword.php");

}

elseif ($password2==NULL)

{

header("location:nopassword2.php");

}

 

 

// If username then password is too short

 

elseif (strlen($username)<5)

{

header("location:shortusername.php");

}

elseif (strlen($password)<6)

{

header("location:shortpassword.php");

}

 

 

// If passwords don't match

 

elseif (!$password == $password2)

{

header("location:passwords.php");

}

 

 

// If username = password

 

elseif ($usernameoo == $password)

{

header("location:same.php");

}

 

 

// If username already exists

 

 

$result = mysql_query("SELECT * FROM profiles");

 

while($row = mysql_fetch_array($result))

{

    if ($row["username"]==$_POST['username'])

{

    header("location:usernameexists.php");

}

}

 

 

// If all is well

 

$encrypt_password=md5($password);

 

 

mysql_query ("INSERT INTO profiles (username, first, last, email, phone)

    VALUES ('$username','$first','$last','$email','$phone')");

 

mysql_query ("INSERT INTO users (user, password)

    VALUES ('$username','$encrypt_password')");

 

 

mysql_close($connect);

 

header("location:complete.php");

 

?>

Link to comment
Share on other sites

It looks like your mysql queries are failing, for the meantime put or die(mysql_error()) after the mysql_queries to see if they are executing correctly. You'll want to remove this line when the site is ready for publishing.


mysql_query ("INSERT INTO profiles (username, first, last, email, phone)
     VALUES ('$username','$first','$last','$email','$phone')") OR die("1" . mysql_error());
         
mysql_query ("INSERT INTO users (user, password)
     VALUES ('$username','$encrypt_password')") OR die("2" . mysql_error());
   

mysql_close($connect);

header("location:complete.php");

 

Or you could have something like the following

$sql1 =  "INSERT INTO profiles (username, first, last, email, phone)
     VALUES ('$username','$first','$last','$email','$phone')";
         
$sql2 = "INSERT INTO users (user, password)
     VALUES ('$username','$encrypt_password')";

if (@mysql_query($sql1) && @mysql_query($sql2))
{
mysql_close($connect);

header("location:complete.php");
}
else
{
//error with mysql query. Change this to a nice neat error when production site is live
exit(mysql_error());
}


Link to comment
Share on other sites

Calling header('Location: ...') does not immediately redirect; the remainder of the page is still executed.  You should call exit immediately after the call to header if you want processing to stop and the redirect to occur at that point.

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.