1

I am getting some information from the login page form.

When I press submit, it goes to a check-login.php page where there it checks from the database if the credentials are correct or wrong.
Then it redirects to a track page.

My problem is that when I press submit on the log in page with the correct credentials.
It redirects to a white page.
And then, if I press refresh, it redirects to the correct page.

<div>
  <h1>Login Form</h1>
  <form action="check-login.php">
    <input type="text" name="user" placeholder="user">
    <input type="password" name="password" placeholder="password">
    <input type="submit" value="log in">
  </form>
</div>

This is the check-login php page

<?php
session_start();
$user=$_GET['user'];
$pass=$_GET['password'];
include("dblogin.php");
$sql="SELECT * FROM login";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if($user==$row["username"] && $pass==$row["password"]){
        $_SESSION["userid"]=$row["id"];
        header ("Location: tracks.php");
  }
}
if ($_SESSION["userid"]==""){
  header ('Location: login.html?message=Wrong Username or Password');
}
?>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Achilleas
  • 23
  • 5

3 Answers3

0

Try the following code to make a redirect:

http_response_code(302); // send redirect code
header('Location: /tracks.php'); // send Location header
exit(0); // don't send anything else
  • if i put the correct credentials its white screen and if i refresh it goes to tracks php.the problem is there – Achilleas Mar 28 '17 at 18:32
0

Try this,

If (!isset($_SESSION['userid'])
{
echo "script type='text/javascript'>window.location.href='login.html?message=Wrong Username or Password'/script";
}

There can be issue due to header function as sometime it doesn't work on some servers.

Note: complete script tag as I'm not able to add code from phone and script tags getting parsed.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Sonal Khunt
  • 1,876
  • 12
  • 20
0

I'm not sure this is the issue...
But even if it is not the fix, it can't be a bad thing to do.

Change your form tag like this:

<form action="check-login.php" method="post">

Then in you PHP change it to $_POST variables like this:

$user=$_POST['user'];
$pass=$_POST['password'];

And I strongly suggest again to ugrade your code from mysql to mysqli or prepared statements. There is plenty answers on SO about this.




EDIT
Previous changes I suggested are good.
But wasn't the issue.

An old memory came to me, as I already had a similar problem in the past.

The issue is that your $_SESSION["userid"]=$row["id"]; doesn't have the time to be written before the tracks.php gets accessed.

So do it like this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if($user==$row["username"] && $pass==$row["password"]){
        //$_SESSION["userid"]=$row["id"];
        header ("Location: tracks.php?id=" . $row["id"]);
  }
}

And in your tracks.php, set it to session.

$_SESSION["userid"]=$_REQUEST["id"];

$_REQUEST will catch $_POST and $_GET.




EDIT: Switch to mysqli

Try this... Even if it is not the fix we're after, it will be a good thing done.
You should apply this modification to all your PHP files.

I know mysql_query has security issues (but not exactly what)... And there may be performance issues too. So it's a good improvement to have at this point.
We'll then be sure the problem source isn't due to this.

<?php
session_start();
$user=$_REQUEST['user'];
$pass=$_REQUEST['password'];
//include("dblogin.php");


$conn = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_error()) {
  echo "<p>Connection failed:".mysqli_connect_error()."</p>\n";
}

$sql="SELECT * FROM login";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

$idFound=false;

while ($row = mysqli_fetch_array($result)) {
    if($user==$row["username"] && $pass==$row["password"]){
        //$_SESSION["userid"]=$row["id"];
        $idFound=true;
        $idFoundis = $row["id"];
        break;
  }
}
if ($idFound){
  header ("Location: tracks.php?id=" . $idFoundis);
}else{
  header ('Location: login.html?message=Wrong Username or Password');
}
?>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64