Gazan Posted February 2, 2009 Share Posted February 2, 2009 Hey there. I've got a problem with the login system im creating. I'm really new to classes and objects, so i suppose it's there the problem exists.. Anyway, my problem is; when i hit login in the login form, it won't log me in before i hit it the 2nd time. So i have to hit login twice before it logs me in, and that happens everytime.. Heres the code for index.php (login page) and classes.php (self describe..) Classes.php: <?php class User { function display_userpanel() { print "Welcome to the user panel.<br /> <a href='logout.php'>Log ud</a>"; } function display_loginform() { require_once("loginform.html"); } function process_login() { if(isset($_POST['brugernavn'])) { mysql_connect("localhost", "root"); mysql_select_db("underskrift"); $query = mysql_query("SELECT * FROM admin WHERE admin_brugernavn ='".$_POST['brugernavn']."' AND admin_password = '".$_POST['password']."' LIMIT 1"); $result = mysql_num_rows($query); if($result==1) { $_SESSION['in_user_id'] = true; } } } function process_logout() { session_start(); session_destroy(); header('location: index.php'); } function logged_in() { if(isset($_SESSION['in_user_id'])) { return true; } else { return FALSE; } } } $User = new User; ?> And the login page (Index.php): <?php require "classes.php"; session_start(); if($User->logged_in()) { $User->display_userpanel(); } elseif($User->process_login()) { } else { $User->display_loginform(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143449-login-system-not-working-optimally/ Share on other sites More sharing options...
MadTechie Posted February 2, 2009 Share Posted February 2, 2009 try thid <?php require "classes.php"; session_start(); //if not logged in and login check fails display form, else user panel if((!$User->logged_in()) && $User->process_login()) { $User->display_loginform(); }else{ $User->display_userpanel(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143449-login-system-not-working-optimally/#findComment-752477 Share on other sites More sharing options...
printf Posted February 2, 2009 Share Posted February 2, 2009 the function process_login() doesn't return anything so most likely they are kicked back to the form because you don't test the return from that function. Combine logged_in() process_login() in your first if() codition and setup process_login() to return true or false... <?php class User { function User () { session_start (); } function display_userpanel () { echo "Welcome to the user panel.<br /><a href='logout.php'>Log ud</a>"; } function display_loginform () { require_once 'loginform.html'; } function process_login ( $return = false ) { if ( isset ( $_POST['brugernavn'] ) ) { mysql_connect ( "localhost", "root" ); mysql_select_db ( "underskrift" ); /* you should use a hash for password storage */ $query = mysql_query ( "SELECT COUNT(*) AS total FROM admin WHERE admin_brugernavn = '" . mysql_real_escape_string ( $_POST['brugernavn'] ) . "' AND admin_password = '" . mysql_real_escape_string ( $_POST['password'] ) . "';" ); $found = mysql_fetch_assoc ( $query ); if ( $found['total'] == 1 } { $_SESSION['in_user_id'] = true; $return = true; } } return $return; } function process_logout () { $_SESSION = array (); session_destroy (); header ( 'location: index.php' ); exit (); } function logged_in () { return isset ( $_SESSION['in_user_id'] ) ? true : false; } } $User = new User (); ?> <?php require 'classes.php'; if ( true === $User->logged_in () || true === $User->process_login() ) { $User->display_userpanel (); } else { $User->display_loginform (); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143449-login-system-not-working-optimally/#findComment-752484 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.