PHP Log in Needs to be Refreshed to Work?

brandon

New member
I use a PHP/MySQL login on my site. The problem I'm having is that the user has to log in twice for it to register correctly and display the $_SESSION variables. My code below:

<?php
$username=$_POST['username'];
$password=$_POST['password'];

$con=mysql_connect("bill","example","examplepass") or die("Could not connect to the server!");
mysql_select_db("Example_DB") or die("Could not connect to the database!");

if($username==""){
echo "You forgot to supply your username!";
} else if($password==""){
echo "You forgot to supply your password!";
} else {
$username=stripslashes($username);
$username=strip_tags($username);
$username=mysql_real_escape_string($username);
$password=stripslashes($password);
$password=strip_tags($password);
$password=mysql_real_escape_string($password);
$query=mysql_query("SELECT * FROM Example_Table WHERE Username='$username' and Password='$password'") or die("Could not complete the first query!");
$result=mysql_num_rows($query);

if($result==1){
session_start();
$_SESSION['username'] = "$username";
header("Location:http://www.slicktak.com/login/login_success.php/");
} else {
echo "Wrong Username or Password!";
}
}

?>

I'm a new programmer when it comes to server-side stuff such as these two languages. However, I'd love it if someone could possibly give me an answer on why the user must refresh and then enter their information again to log in. Thank you.

P.S. For some reason; throughout the code on here it replaces some of it with "..." e.g. "e..." or "slicktak...".
 
Back
Top