tags:

views:

50

answers:

2

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>
+1  A: 

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.

darthnosaj
I changed that but still have the same problem. Any ideas?
Okay, I may be missing something, but when you are using session_register(), why are you registering "username"? If I understand session_register() correctly, you are only setting a session variable called "username" that has no value. Maybe try putting "myusername" (which is your variable with the value from the username post) instead of "username". I have never used session_register() before, so I could be wrong.
darthnosaj
According to the php manual, session_register() is deprecated anyway. I would try using $_SESSION["username"] = $myusername instead. I hope this helps!
darthnosaj
Sorry to keep going, but I just noticed something else. You have to call session_start() on every page you want to use session variables on. It needs to go before any html output as well.
darthnosaj
Thanks for the continued help. I have tried everything you have suggested but it is not even saying the user name is wrong if I enter it incorrectly. I'm not even sure if the PHP is working.
If you are not seeing the <?php tags rendered on your page, then PHP should be working correctly. Here is a shot in the dark. What is the filename of the code you posted above? Your form is posting to LoginReal.php, so I thought it would be good just to be sure. The only other thing I noticed that threw me off was your using of the $tablename variable in your query. I don't see it defined anywhere. Are you sure this is correct?
darthnosaj
A: 

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?

jeroen