I'm trying to create a simple log in page. I have a register page and when the user signs up and then attempts to log in it will navigate them to the homepage. However When the user attempt to log in and there user name and password is not in the database i would like the script to navigate them to the sign up page.
I'm having troubles with the else statement, can anyone help me with this issue?
This is the script...
function SignIn()
{
session_start(); //starting the session for user profile page
if(!empty($_POST['userName']))
{
$query = mysql_query("SELECT * FROM users where userName = '$_POST[userName]' AND createPassword = '$_POST[createPassword]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['userName']) AND !empty($row['createPassword']))
{
$_SESSION['userName'] = $row['createPassword'];
echo "<script>window.location = 'index.php'</script>";
}
else
{
echo "<script>window.location = 'signUp.php'</script>";
}
}
}
if(isset($_POST['submit']))
{
SignIn();
}
?>
EDIT: Hi thanks for your feedback, I have changed the query and edited the echo. Thanks for the security tips but in this script security isnt an issue. I only need it to direct you to the index if you have signed up and to the register if you haven't. I still cant get the page to redirect to the sign up page if the log in and pass word is incorrect.
If its correct it takes you to the index however if wrong it presents the php page this script is on.
This is my current attempt
function SignIn()
{
session_start();
if(!empty($_POST['userName']))
{
$query = mysql_query("SELECT * FROM users where userName = '{$_POST['userName']}' AND createPassword = '{$_POST['createPassword']}'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['userName']) AND !empty($row['createPassword']))
{
$_SESSION['userName'] = $row['createPassword'];
header("Location:http://localhost/waves/index.php");
}
else if (!empty($row['userName']) AND !empty($row['createPassword']))
{
$_SESSION['userName'] != $row['createPassword'];
header("Location:http://localhost/waves/signUp.php");
}
}
}
if(isset($_POST['submit']))
{
SignIn();
}
?>