Users cannot log in, what is wrong?
Okay I have tried to make a login system with users. But my users cannot log in. And well that is a problem.
My login page:
<?
session_start();
include "database.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Wildsoul</title>
</head>
<body>
<div id="welcome">
<h3>Log ind.</h3>
<br><br>
<p><form action="login-ok.php" method="post">
<input type="text" name="username"><br>
<input type="password" name="password"><br><br><br>
<input type="submit" name="login" value="Login">
</form></p>
<br /><br />
</div>
</div>
</body>
</html>
The login-ok page:
<?
session_start();
include "database.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
<div id="welcome">
<h3>Log in</h3>
<p><?php // PHP script start.
require("config.php"); // Variabler til mysql hentes
$login = &$HTTP_POST_VARS['login'];
$_SESSION['login'] = $login; //AF rettelse
if(empty($login)) {
echo 'You need to log in'; }
else {
$username = &$HTTP_POST_VARS['username']; // Brugernavn fra form i login.php.
$password = &$HTTP_POST_VARS['password']; // Password fra form i login.php.
if(empty($username) OR empty($password)) { //Her tjekkes om ET af felterne er tomme.
echo 'Et af felterne er tomme'; }
else { // Denne klamme starter alt script der udføres HVIS begge felter er udfyldte.
// Her åbnes for mysql-serveren og der logges ind med data fra config.php.
mysql_connect($mysql_host, $mysql_user, $mysql_pw);
mysql_select_db($mysql_db); // Der åbnes for databasen defineret i config.php.
$result_pw = mysql_query("select password from users where brugernavn = '$username'") or die (mysql_error());
$result_user = mysql_query("select brugernavn from users where password = '$password'") or die (mysql_error());
// Her sker det egentlige tjek om brugernavn og password er dem som står i databasen.
// Der oprettes først et array med de data vi fik fra mysql før.
$array_user = mysql_fetch_array($result_user); // Først et med brugernavn.
$array_pw = mysql_fetch_array($result_pw开发者_Python百科); // Og et med password.
// Her tjeckes så om både array med brugernavn og password indeholder data
// magen til dem skrevet i felterne i html-formen
// PASSWORD KONTROL
if($array_user['username'] == $username AND $array_pw['password'] == $password) {
echo 'correct password. <a href="http://wildsoul.dk/indeks.php">Klik her</a>.'; // Brugernavn og password passede!!
$_SESSION['username'] = $username; //AF rettelse
$_SESSION['password'] = $password; //AF rettelse
} // Her lukker vi condition TRUE fra password kontrol
else { // Her starter condition FALSE på password kontrol
echo 'forkert password';
} // Her lukkes condition FALSE på password kontrol.
} // Her lukker vi for condition FALSE på tomme-felter-tjek.
}
?>
</p>
<br /><br />
</div>
</div>
</body>
</html>
And when I have to check if the user has logged in, I have this code:
<?
session_start();
include "database.php";
?>
<? if(isset($_SESSION['username']) && isset($_SESSION['password'])){ ?>
bla bla bla
<?}
else{
echo 'You have to log in'; }
?>
Where am I going wrong here?
It looks like you have a logic problem, you're checking $login before actually trying to validate the user. Remove that and just try validate them and then set your session variables.
So remove the if(empty($login)) ... and just connect to your db etc. If that doesn't work, try editing your post with what problems you're getting besides "it doesn't work" :)
- You have spaces before first
<?
so session cannot be started successfully - it might be your whole problem - Do not use short starting PHP tags
<?
but use<?php
精彩评论