Authentication problem

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Authentication problem

Post 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>
User avatar
fang
Posts: 31
Joined: Sat Feb 04, 2012 3:17 am

Re: Authentication problem

Post 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'])) {
Python/PHP/Javascipt . HTML/CSS . MySQL/CouchDB . Apache/Nodejs . git/sh/ssh . Windows/MacOSX/Linux
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Authentication problem

Post 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.
The indelible lord of tl;dr
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Authentication problem

Post 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.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Authentication problem

Post 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.
Post Reply

Return to “Beginner Help and Support”