Jump to content

OOP Basic User Login / Unsure Where I'm Going Wrong


Recommended Posts

Basically, I'm still trying to wrap my head around OOP. What I'm trying to do here is a simple OOP user login script.  But when I submit the form, all that happens is that the text fields reset them selves and nothing that I feel should be happening, happens.

 

ie: I submit login data, and either it displays an error or reirects to index page. Neither happen, the form merely resets.  Where am I going wrong?

 

<form name="loginform" id="loginform" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>
	<label>Username<br>
	  <input name="user" id="user_login" class="input" size="20" tabindex="10" type="text" />
	</label>
</p>
<p>
	<label>Password<br>
	<input name="pass" id="user_pass" class="input" value="" size="20" tabindex="20" type="password"></label>
</p>
<p class="forgetmenot"><label><input name="rememberme" id="rememberme" value="forever" tabindex="90" type="checkbox"> Remember Me</label></p>
<p class="submit">
	<input name="login" id="submit" class="button-primary" value="Log In" tabindex="100" type="submit">
	<input name="redirect_to" value="/users.php" type="hidden">
</p>
</form>

 

<?php
if(isset($_POST['login']))
{
$username = $_POST['user'];
$password = $_POST['pass'];

include("./classes/class.users.php");
USERS::login($username, $password);

}
?>

 

<?php
// Yes, my DATABAASE::DoIT(1) // (0) is working as intended (from a different include file)
class USERS
{
var $user;
var $pass;
var $email;

//////////////////////////////////////////////////////////////////////////////////////////////
function login($user, $pass) {

	include("/var/www/config.php");
	DATABASE::DoIt('1');

	$hashword = sha1($CONFIG['salt1']."$pass".$CONFIG['salt2']);
	$sql      = "SElECT * FROM users WHERE username='$user' AND hashword='$hashword'";
	$result   = mysql_query($sql);
	$count    = mysql_num_rows($result);

	if($count==1) {
		while ($row = mysql_fetch_assoc($result)) {
			define('USERS_AUTHENTICATED', true);
			$_SESSION['USERS_username'] = $row['username'];
			$_SESSION['USERS_userid'] = $row['userid'];
			DATABASE::DoIt('0');
			header("Location: index.php");
		}

	} else {

		$_SESSION['loginError'] = true;
		DATABASE::DoIt('0');
		return $_SESSION['loginError'];
	}

	DATABASE::DoIt('0');
}
}
?>

 

Your login() function also uses a while loop that is required.

 

I don't understand what you mean by this. The while loop is there to populate the session vars, or should I be doing it a different way?

I've changed the call as per your suggestion, but still having the same issues. :s

 

As a side note: I have not overlooked string cleanup - I just have not yet implemented it. It's on the to-do list though! :)

I don't understand what you mean by this. The while loop is there to populate the session vars

 

Your only logging in 1 user, you do not need a loop to get 1 result.

 

I've changed the call as per your suggestion, but still having the same issues. :s

 

I would assume your DATABASE object's doit() method isn't static either. We need to see more code.

 

Your might want to take a look at how classes actually work in php, you seem quite off track.

My database class is static, and looks like the following.

 

As per your remark, Thorpe

Your might want to take a look at how classes actually work in php' date=' you seem quite off track.[/quote']

That's why I'm posting here.  I've read a good many tutorials on OOP, and the book that I have is "PHP and MySQL Web Development" by Welling, Thomas, 3rd Ed.  Can you tell me / Explain what I appear to be missing/not understanding, fundamentally speaking, in terms of the way that OOP works?

 

Like I said, I've very much new to OOP, so any help you can provide would be much appreciated :)

 

Since I'm only trying to pull one row / authenticate one user, I should instead do something more like:

<?php
		// We won't use a WHILE loop, as we're only pulling one row...
		$info = mysql_fetch_array($result);
		define('USERS_AUTHENTICATED', true);
		$_SESSION['USERS_username'] = $info['username'];
		$_SESSION['USERS_userid'] = $info['userid'];
		DATABASE::DoIt('0');
		header("Location: index.php");
?>

DATABASE class

<?php
class DATABASE {
function DoIt($option) {

	if ($option = 'yes' || 'connect' || '1' || 'open') {
		mysql_connect( 'localhost', 'fuzzyUser', 'SuperS3cr3t*' ) or die("shit broke on the database connection");
		mysql_select_db('media') or die("Selecting the database failed. Pat fails.");
	} else if ($option = 'no' || 'close' || '0' || 'disconnect') {
		mysql_close();

	} else { 
		die('unknown database error');
	}
} // Close easy DB Functionality
} // Close our DATABASE class
?>

Like Thorpe said, you're calling your methods wrong.  :: is the scope resolution operator.  When used in the manner you're using, it means one is trying to invoke a static method.  Neither of your methods are static.

 

Am I missing a fundamental definition of what static means?  /me scratches head

The DATABASE class, as far as I would have though, is/was static - simply  because it really doesn't do anything other than invoke a database connection/kill it. 

 

Given that neither are static, I should be calling my DATABASE connection as...

<?
$connect = new DATABASE;
$connect->DoIt('1');
?>

 

Just like I'm calling my Login code currently..  correct?  :shrug::confused:

<?
include("./classes/class.users.php");
$user = new USERS;
$user->login($username, $password);
?>

Like Thorpe said, you're calling your methods wrong.  :: is the scope resolution operator.  When used in the manner you're using, it means one is trying to invoke a static method.  Neither of your methods are static.

 

Am I missing a fundamental definition of what static means?

 

Yes, you are: http://php.net/manual/en/language.oop5.static.php

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.