Jump to content

Recommended Posts

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();
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/143449-login-system-not-working-optimally/
Share on other sites

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();
}

?>

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 ();
}

?>

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.