Page 1 of 1

Authentication problem

Posted: Sun Feb 05, 2012 6:14 am
by Nexus
Well, I am having a problem with my authentication as I am getting a blank page every time I login

Code: Select all

<!DOCTYPE html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
include_once('connect.php');
session_start();

if(isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $username = strip_tags($username);
    $password = strip_tags($password);
    $password = md5($password);
    
    $query = "SELECT username,password from players where username='$username' and '$password'";
    $result = mysql_query($query) or die(mysql_error());
    $result2 = mysql_fetch_array($result);
    
if($result2) {
    $_SESSION['username'] = $username;
    echo "You have logged in successfully!<br><a href='index.php'>Login</a>";
}

if($username != $result2['username'] || $password != $result2['password']) {
    echo "Username or password was incorrect. <br><a href='home.php'>Go Back</a>";
}
}
?>
</body>
</html>
my login section of the home page

Code: Select all

<div id="login">
<form method="post" action="authenticate.php">
Username:<input type="text" name="username" size="21" /><br/>
Password:<input type="password" name="password" size="32" />
<input type="submit" value="Login" /><br/>
</form>
<a href="register.php">Register</a>
</div>

Re: Authentication problem

Posted: Sun Feb 05, 2012 6:42 am
by fang
At first glance, I think your problem is here:

Code: Select all

if(isset($_POST['submit'])) {
I don't remember at the moment if unnamed form elements don't get sent in the post request or if php just doesn't pick up on them, but either way you'll need to change

Code: Select all

<input type="submit" value="Login" /><br/>
to

Code: Select all

<input type="submit" name="submit" value="Login" /><br/>
or check something else, such as:

Code: Select all

if(isset($_POST['username']) && isset($_POST['password'])) {

Re: Authentication problem

Posted: Sun Feb 05, 2012 7:08 am
by Jackolantern
I know this sounds silly, but also check to make sure there is actually something in the file it is leading to. Failure to a blank page is rare in PHP unless you have unknowingly created some logic on the page that can bypass all of the page's content. You will typically get some kind of error message in other cases.

I have actually found two different IDEs that have bugs that can cause them to incorrectly save files, leaving them at 0 bytes (Code Lobster and phpDesigner), so I figure it is possible more IDEs may do it, too, sadly. One night I spent hours trying to debug a blank page issue, only to find that the actual file saved was 0 bytes due to an IDE bug, even though the full file contents were still loaded in the IDE and appeared to work correctly.

Re: Authentication problem

Posted: Sun Feb 05, 2012 3:08 pm
by Nexus
fang thanks man that works and made sense as it didn't have an element to put through because of that. Stupid mistake in my opinion hahaha but I will progress and try to get as good as I was in the summer months.

Re: Authentication problem

Posted: Sun Feb 05, 2012 3:43 pm
by Xaleph
Hey just on a sidenote, the best method to see if a POST occured is using the $_SERVER`s global var.

Code: Select all

if( $_SERVER['REQUEST_METHOD'] == 'POST'){

   // a post occured
}
This has the advantages that you don`t need to add hidden fields or weird form attributes to check if a post occured. However, it`s still recommended you check the right values once inside the if.