php wont log me in
Posted: Wed Aug 05, 2009 1:51 am
Hi, i'm learning PHP through the book 'php and MySQL Web Development' and im trying to make a log in system and for some reason i cant get it to work. I have tried a simpler version on my local machine not connecting to a database and it works fine, but this version is looking for a username and password in a database, but i cant log in, any ideas why?
Thanks for looking.
Ben.
Code: Select all
<?php
session_start();
if (isset($_POST['userid']) && isset ($_POST['password'])) {
//if the user has just tried to log in;
$userid = $_POST['userid'];
$password = $_POST['password'];
$db_conn = new mysqli('my.server.com', 'username', 'password', 'database');
if (mysqli_connect_errno()) {
echo 'connection to the database has failed: '.mysqli_connect_error();
exit();
}
$query = 'selsect * from authorized_users '
."where name = '$userid' "
."and password =sha1('$password')";
$result = $db_conn->query($query);
if ($result->num_rows > 0) {
//if they are in the database register the user id;
$_SESSION['valid_user'] = $userid;
}
$db_conn -> close();
}
?>
<html>
<body>
<h1>Home Page</h1>
<?
if (isset($_SESSION['valid_user'])) {
echo 'you are logged in as ' . $_SESSION['valid_user'] . '<br />';
echo '<a href = "logout.php">log out</a><br />';
}else{
if (isset($userid)) {
//if they've tried to log in and failed;
echo "could not log you in";
}else{
//they have not tried to log in yet;
echo "you are not logged in";
}
//privide for to log in;
echo '<form method="POST" action="authmain.php">';
echo '<table>';
echo '<tr><td>Userid: </td>';
echo '<tr><input type="text" name="userid"></td></tr>';
echo '<tr><td>Password: </tr>';
echo '<td><input type="password" name="password"<td>';
echo '<tr><td colspan="2" align="center" >';
echo '<input type="submit" value="log in"></td></tr>';
echo '</table></form>';
}
?>
</body>
</html>
Ben.