-1

I am having a login page which has a form. The login data are send into a php script to check whether the user exists in the DB. If successful, it should redirect.

Here is my code:

<?php

$host="localhost:8889"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="TableName"; // Database name
$tbl_name="Users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file
  session_register("myusername");
  session_register("mypassword");
  header("location:../main.php");
  die();
}
else {
  echo "Wrong Username or Password";
}
?>

Count is 1, but redirection does not happen.

I have read this great answer and checked the following:

  1. there is not empty space before the opening tag, nor I am having another echo or print.
  2. i tried after the opening php tag to put ob_start but it didn't work either.

I didn't understand the thing with BOM. Can you help me on that?

I have been using the exact same code in the past and it worked and now that I am on mamp and mac does not work.

Thanks.

EDIT:

I did what people have said and after fixing these things and enabling reports, and the problem is that:

Call to undefined sesion_register.

So, I changed that as it is not defined nor and it is deprecated and it works now! Thanks all!

Community
  • 1
  • 1
ghostrider
  • 5,131
  • 14
  • 72
  • 120

3 Answers3

1

Try this:

header("Location: http://localhost:8889/main.php");

Location should have a capital L, and it's always good to try using an absolute URI.

EDIT

After reading this link it might be a good idea to check that output_buffering is enabled in your php.ini. Then place ob_start() and ob_end_flush() at the top and bottom of your file respectively.

Community
  • 1
  • 1
  • and with a space after `:`. Normally I will call `exit()` after `header('Location: ...')`, instead of `die()`, though they work the same. – Raptor Nov 04 '13 at 03:33
  • Although this answer demonstrates good technique, it's not likely to actually solve OP's problem. HTTP headers are case-insensitive, the space is optional, and all browsers support relative URLs even though it's not in the spec. – Boann Nov 04 '13 at 03:37
  • 1
    http://stackoverflow.com/questions/12033257/php-header-not-working-in-mamp-even-after-removing-white-space This seems to be almost an exact duplicate, even though it was asked by someone else. Even the code is similar. –  Nov 04 '13 at 03:40
0

For starters, you're not using your variables. You're using actual values in quotes. Instead of:

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

It should be:

mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
napo
  • 869
  • 9
  • 19
  • 3
    Actually, as long as you're using double quotes, PHP will put in the appropriate values. In single quotes, it will output the literal string. –  Nov 04 '13 at 03:35
  • Ok but that's not the problem If I place an echo inside the if condition it is printed. – ghostrider Nov 04 '13 at 03:35
0

Please capitalize "L" on the location. If this not work try the following;
You can check for errors using headers_sent()

// If no headers are sent, send one
if (!headers_sent()) {
    header('Location: ../main.php');
    exit;
}


Or you can try javascript on php

<?php
echo "<script>window.location='../main.php'</script>";
?>

Hope it helps...

Shudmeyer
  • 354
  • 2
  • 14