-1

I want user to land on a different page, if login is successful which is not working. Please have a look at the below code and assist me on the issue.

$result = mysql_query("SELECT * FROM users where EMAIL='$username' and PASSWORD='$password'", $link);
$num_rows = mysql_num_rows($result);
if ($num_rows==0)
    echo "<h4><center>Invalid Username or password. Please <a href='login.php'>click here</a> to go back to Login page</center></h4>";
else
{
    header('Location: http://xxxx/test.php');
}

Please help me out. Thanks in advance

Alex
  • 8,461
  • 6
  • 37
  • 49
Saibalu
  • 113
  • 2
  • 12
  • @яша : Thanks for your reply, i want to redirect user to test.php page if the login is successful which is handled in the else block. But redirect is not happening. Thanks – Saibalu Nov 28 '15 at 15:35
  • what do you want to achieve precisely and in how far ist the way this behaves different from the way you want it to act – JRsz Nov 28 '15 at 15:36
  • have you checked if this location is alive ? – Yehia Awad Nov 28 '15 at 15:37
  • and are you caertain that the result has some values in it? Have you tried var_dump($result); ? – JRsz Nov 28 '15 at 15:38
  • @JRsz : Hi, if username and password entered by the user matched to the database then he should be redirected to the test.php page; This is what i want to achieve. Thanks – Saibalu Nov 28 '15 at 15:38
  • I know, but maybe the redirect is fine, you just do not get any results – JRsz Nov 28 '15 at 15:39
  • @JRsz : Yes, the values are coming perfectly, i tried to echo those variables. But the only problem which i am facing is rather than going to test.php page, i will be in the same page where i enter the info thanks – Saibalu Nov 28 '15 at 15:40
  • http://php.net/manual/en/function.error-reporting.php that will tell you if you're outputting before header, something I think is the cause. – Funk Forty Niner Nov 28 '15 at 15:50
  • You should also brace this `if ($num_rows==0)` etc. `if ($num_rows==0){...}` – Funk Forty Niner Nov 28 '15 at 15:50

1 Answers1

0

Try this:

header("Location: http://example.com/test.php");
die(); // end script execution here will "enable" above redirect

Explained in more detail here

Community
  • 1
  • 1
  • This is the proper way indeed, but should nevertheless work without die(). (Though you should always call this function after a header() call) – JRsz Nov 28 '15 at 15:40
  • Hi, I tried the same as per your suggestion but still its not working. Do i need to change any settings to make the header works on my hosting server? – Saibalu Nov 28 '15 at 15:45
  • @Saibalu parhaps you have some header values already set in some file which is executed previous to the one where you are doing redirect. Even a one empty line of HTML sets header values, so perhaps there is even a single blank space after ?> ending php tags somehwere in your code running previous to this header location redirect try... investigate a bit... – Ivan Veštić Nov 28 '15 at 15:52
  • HI All, I used instead of header tag and its working now. But still i dont know what might be the root cause for header not working. Really thankful to each of you people for assisting me on this issue. Thanks a lot – Saibalu Nov 28 '15 at 16:02
  • @Saibalu sure thing. Have you checked that you used full url with protocol e.g http:// as prefix like in my answer example above e.g. Location: http://example.com. Or did you just use Location: example.com ? – Ivan Veštić Nov 28 '15 at 16:11