I’ve put together a login script, and by put together I mean I copy and pasted bits and pieces of code from forums and got it working from trial and error.
So far the script is working fine, when logging in there is no issue at all, the only problem comes from when you try to login without filling in any of the fields, it gives me some strange error and redirects me to a blank page with
Object Moved
This document may be found here
It’s not a huge issue as I don’t see why people would try and login with blank fields anyway, but it’s something I’d like to know how to solve for future reference.
Below is the login portion of my code:
<?php
session_start();
$data=array("user1"=>array("url"=>"page1.php","password"=>"12345"),
"user2"=>array("url"=>"page2.php","password"=>"12345"),
"user3"=>array("url"=>"page3.php","password"=>"12345")
);
if(isset($_POST['username']) && isset($_POST['password'])) {
$user = strtolower($_POST['username']);
$pass = strtolower($_POST['password']);
if($data[$user]['password'] == $pass) {
$_SESSION['username'] = $user . " " . $pass;
header('Location: ' . $data[$user]['url']);
} else {
echo '<script type="text/javascript">window.onload = function() { document.getElementById("loginError").innerHTML = "' . "Invalid login details. Please try again." . '"; }</script>';
logIn();
}
} else {
logIn();
}
?>
Ensure or change that the `header(‘Location: ‘ . $data[$user][‘url’]); is an absolute path when redirecting and not a relative path. See the related post as proof for my answer PHP on IIS7 – Receiving "Object Moved" html page instead of actually redirecting.
To fix that blank password issue change the check to the following: