PHP isn’t my thing but I need to set up a simple login area–so here I am. Here’s some code I found that seems to work, but won’t redirect.
If I change the redirect to an echo it works. If I change the location to “include” instead it works. What am I doing wrong in the “location” code?
Am open to suggestions on alternately coding this. I have a single webpage that if the user enters a password an additional section on the site will appear. So I have a form on that site submitting to this code and then it’s supposed to redirect back with the new section now visible.
Here’s my code–
<?php
// error reporting
ini_set('display_errors',1);
error_reporting(E_ALL);
$host="localhost"; // Host name
$username="foo"; // Mysql username
$password="foo"; // Mysql password
$db_name="foo"; // Database name
$tbl_name="members"; // 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){
header("Location: http://www.google.com/");
// Register $myusername, $mypassword
// success
//session_register("myusername");
//session_register("mypassword");
}
else {
echo "Wrong Username or Password";
}
?>
session_registeris deprecated. Hence output already send which you could have seen had error reporting was on. Nothing can be output beforeheader