php - Can't redirect from login page to home page -


this question has answer here:

i learning php. have made login page. problem facing here that, when user clicks on signin button & if record found taken other page displays redirect link, user has click on go next page. want when user click on signin button, details should cross checked in database, if record found user should directly redirected next page else error should displayed. html page:

<!doctype html> <html>     <head>         <title>openmoz</title>         <meta charset="utf-8"/>          <meta name="viewport" content="width=device-width, initial-scale=1">         <link rel="stylesheet" type="text/css" href="index.css"/>         </head>     <body  style="height:650px;">     <h1 align="center" ><b><i>city login</i></b></h1>         <div class="login">         <form action="login.php" method="post">             <input type="text" placeholder="username" name="username" autocorrect=off autocapitalize=words required> <br>              <input type="password" placeholder="password" name="password" autocorrect=off autocapitalize=words required> <br>             <a href="home.php"><input type="submit" name="submit" value="sign in"></a>         </form>          <a href="signup.php"><input type="submit" name="submit" value="sign up"></a>         <div>       </body> </html> 

this login.php script verify details :

<?php $username = $_post["username"]; $password = $_post["password"];  if($username && $password) {     $connect = mysql_connect("localhost","root","password") or die("couldn't connect");     mysql_select_db("phplogin")or die("couldn't connect");     $query = mysql_query("select * users username='$username'");     $numrows = mysql_num_rows($query);     if($numrows!=0)     {         while($row = mysql_fetch_assoc($query))         {             $dbusername = $row['username'];             $dbpassword = $row['password'];         }         if($username==$dbusername && $password==$dbpassword)         {             echo ("<center><a href='home.php'>redirect</a></center>");             $_session['username'] = $username;         }         else         {             echo ("incorrect password !");         }        }     else         die("the user doesn't exist");  } else     echo ("please enter username & password");  ?> 

i thankful if problem gets solved.

as long have not outputted browser, can header redirect. achieve aim.

change this:

echo ("<center><a href='home.php'>redirect</a></center>"); $_session['username'] = $username; 

to this:

$_session['username'] = $username; header("location: /some-new-page.php"); exit; 

always exit; after location redirect.

oh yeah, , clean inputs.. ..you wide open sql injection.

$username = mysql_real_escape_string($_post["username"]); $password = mysql_real_escape_string($_post["password"]); 

oh yeah .. , mysql_* deprecated. use mysqli_*


Comments