Hey,
So I have implemented a login script to my site. The only problem is when it loads it refreshes the page and displays "Welcome $username! You are logged in! Visit the Galleries Page!"
I tried using a "header('Location: galleries.php');" to redirect on login but I couldn't get it to work.
Here is my login page
login.php
<?php
require_once('common.php');
$error = '0';
if (isset($_POST['submitBtn'])){
// Get user input
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
// Try to login the user
$error = loginUser($username,$password);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Galleries</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/login.css" />
</head>
<body bgcolor="000000">
<table id="rotator" border="0" width="100%" height="100%">
<tr>
<td align="center">
<table id="loginbox" border="0" width="339" height="339">
<tr>
<td>
<table border="0" background="" width="280" height="280" align="center">
<form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<tr>
<td colspan="2" height="40px">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<h2>Login</h2>
</td>
</tr>
<tr height="5px">
<td align="center">
<font color="#ffffff">
Username:
</font>
</td>
<td>
<input class="text" type="text" name="username" size="18" maxlength="18">
</td>
</tr>
<tr height="5px">
<td align="center">
<font color="#ffffff">
Password:
</font>
</td>
<td>
<input class="text" type="password" name="password" size="18" maxlength="18">
</td>
</tr>
<tr height="60px">
<td align="center" colspan="2">
<input class="text" type="submit" name="submitBtn" value="Login">
<input class="text" type="reset" name="Clear" value="Clear">
</td>
</tr>
<tr>
<td align="center" colspan="2" height="50px">
<?php
if (isset($_POST['submitBtn'])){
?>
<?php
if ($error == '') {
echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>';
}
else echo $error;
?>
<?php
}
?>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Here is the common.php file
common.php
<?php
session_start();
function registerUser($user,$pass1,$pass2){
$errorText = '';
// Check passwords
if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
elseif (strlen($pass1) < 6) $errorText = "Password is to short!";
// Check user existance
$pfile = fopen("pwd.asp","a+");
rewind($pfile);
while (!feof($pfile)) {
$line = fgets($pfile);
$tmp = explode(':', $line);
if ($tmp[0] == $user) {
$errorText = "The selected user name is taken!";
break;
}
}
// If everything is OK -> store user data
if ($errorText == ''){
// Secure password string
$userpass = md5($pass1);
fwrite($pfile, "\r\n$user:$userpass");
}
fclose($pfile);
return $errorText;
}
function loginUser($user,$pass){
$errorText = '';
$validUser = false;
// Check user existance
$pfile = fopen("pwd.asp","r");
rewind($pfile);
while (!feof($pfile)) {
$line = fgets($pfile);
$tmp = explode(':', $line);
if ($tmp[0] == $user) {
// User exists, check password
if (trim($tmp[1]) == trim(md5($pass))){
$validUser= true;
$_SESSION['userName'] = $user;
}
break;
}
}
fclose($pfile);
if ($validUser != true) $errorText = "<font color='red'>Invalid username or password!</font>";
if ($validUser == true) $_SESSION['validUser'] = true;
else $_SESSION['validUser'] = false;
return $errorText;
}
function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['userName']);
}
function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
header('Location: ../login.php');
}
}
?>
I still want it to display "Invalid username or password!" and the other error's on the login.php page. But I want a successful login to redirect to "galleries.php"
Can anyone help me with this. I can't seem figure this one out.
Thanks!
AW