Php connection to database
I am trying to connect to a database using a login form. Currently there is one user in the database but when pressing submit the page just appears to refresh and is not redirected to the home page as it should. Here is my code:
<html>
<head><title>Login</title></head>
<body>
<?php
ob_start();
include('connect.php');
$handle = mysql_connect($hostname, $username, $password)or die("cannot connect");
$error = mysql_select_db($databasename,$handle);
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tablename WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);开发者_开发百科
if($count==1){
session_register("username");
session_register("password");
header("Location: home.php");
}
else {
echo "Wrong Username or Password";
}
?>
<form action='LoginREAL.php'
method='POST' style='margin: .5in'>
<p><label for='user_name' style='font-weight: bold;
padding-bottom: 1em'>USER ID: </label>
<input type='text' name='username' id='username'
value='' /></p>
<p><label for='password' style= 'font-weight: bold'>Password: </label>
<input type='password' name='password' id='password'
value='' /></p>
<p><input type='submit' value='Login'> </p>
<input type='hidden' name='sent' value='yes'/>
<a href= "/home/jparry2/public_html/register.php">Register</a>
</form>
</body>
</html>
My guess would be the problem is not with your login functionality, but with your header() redirection statement. The header() redirection will only work if it occurs before any html is sent to the browser. Once the html has started, the http headers have already been sent and cannot be changed. Hopefully, that is your only problem.
You cannot use header() after you have sent output to the browser so you need to put the php stuff before the html tag.
By the way, I don´t know how your server is set up, but I don´t think your register link is going to work (I assume that public_html is the root of the server...).
Edit: I see that you are turning output buffering on but you are not flushing the buffer. Is there any specific reason to do that?
精彩评论