php wont log me in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Benjipie
Forum Newbie
Posts: 11
Joined: Sun Dec 07, 2008 7:04 am

php wont log me in

Post by Benjipie »

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?

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>
 
Thanks for looking.

Ben.
User avatar
bala_1225
Forum Commoner
Posts: 29
Joined: Tue Jul 28, 2009 3:20 am
Location: chennai,india

Re: php wont log me in

Post by bala_1225 »

hi u need to register the session variable also...


for eg.,

session_register('valid_user');
$_SESSION['valid_user'] = $userid;


$query = 'selsect * from authorized_users '
."where name = '$userid' "
."and password =sha1('$password')"; ////u done a Spelling mistake


where u declared the num_rows variable....



if (isset($_SESSION['valid_user']))
{
echo 'you are logged in as ' . $_SESSION['valid_user'] . '<br />';
echo "<script type='text/javascript'>window.top.location='index.php';</script>";
}


First u need to divert the page when login successfully.... ther u can give logout concept.....
Post Reply