Jump to content

[SOLVED] Learning PHP - cant get log in system to work.


Recommended Posts

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. 

 

<?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.

Hi bundyxc,

 

Thanks for the reply.  I corrected that (stupid) mistake and can log in, the trouble im having now is i have a 'members only' area (just one page) that for some reason i cant get into, i keep getting told im not logged in even though i start the session on every page. Any ideas what could be stopping me....

 

This is the code for the members only page,

 

<?php
session_start();

if (isset($_SESSION['valid_user'])) {

echo '<h1>WELCOME USER</h1>' . '<br />';
echo '<p>you are logged in as: ' . $_SESSION['valid_user'];
}else{
echo '<p> could not log you in</p>' . '<br />';
echo '<a href = "authmain.php">back</a>';
}


?>

 

The code from the log in page has the link to member_only.php in this 'if' statement

 

if (isset($_SESSION['valid_user'])) {
   	echo '<a href="members_only.php">members section</a>';
    echo 'you are logged in as ' . $_SESSION['valid_user'] . '<br />';
    echo '<a href = "logout.php">log out</a><br />';
}else{

 

Thanks again for the help...

I'm not to familiar with mysqli functions, but I was reading up on the PHP.net page on mysqli_query: http://us.php.net/manual/en/mysqli.query.php

 

maybe try instead of

$result = $db_conn->query($query);

 

use

 

$result = $db_conn->query($query, MYSQLI_USE_RESULT);

 

Can't guarantee that will do anything, but hope that helps

Thanks for the replies guys. 

 

PFMaBiSmAd, i did as you suggested and from what i can gather i dont think the session variable is getting passed, any ideas why this could be?

 

the page is on http://www.eslgroups.com/authmain.php

 

The username is:Ben

The password is: Ben

 

Thanks for the help. 

 

/var/php_sessions/

 

The above is what session.save_path is currently set to.

 

Either session.save_path is set correctly and that path simply does not exist and needs to be created or session.save_path is set incorrectly (it is some default value to a non-existent folder that your host set it to) and it needs to be set to the correct folder that needs to be created for this purpose.

 

If you are on shared web hosting you should set session.save_path to be to a folder that is outside your document root folder (closer to the disk root) so that the session data files are not accessible through http requests.

Thanks again PFMaBiSmAd for all your help. 

 

Im almost there, i can feel it in my bones!! 

 

So i found session.save_path in the php ini file on my host server, what would you recogmend i set it to?  Would

 

session.save_path= "/var/php_sessions/"

 

be okay?

 

Sorry for all the stupid questions..

 

Thanks again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.