benjipie Posted August 5, 2009 Share Posted August 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/ Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Share Posted August 5, 2009 $query = 'selsect Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891110 Share on other sites More sharing options...
benjipie Posted August 5, 2009 Author Share Posted August 5, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891455 Share on other sites More sharing options...
mikesta707 Posted August 5, 2009 Share Posted August 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891462 Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2009 Share Posted August 5, 2009 Add the following two lines of code immediately after the first opening <?php tag on both pages - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891469 Share on other sites More sharing options...
benjipie Posted August 5, 2009 Author Share Posted August 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891486 Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2009 Share Posted August 5, 2009 /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. Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891491 Share on other sites More sharing options...
benjipie Posted August 5, 2009 Author Share Posted August 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891501 Share on other sites More sharing options...
benjipie Posted August 5, 2009 Author Share Posted August 5, 2009 Thank you so much PFMaBiSmAd!! It worked!! Quote Link to comment https://forums.phpfreaks.com/topic/168897-solved-learning-php-cant-get-log-in-system-to-work/#findComment-891516 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.