Jump to content

OOP - What makes this method not static?


maxudaskin

Recommended Posts

I removed the last half of the file, but what makes the login($username, $password) function not static? I want to access it statically, but I am getting an error. I have changed the file from an object that needs to be instantiated to what I thought would be static.

 

Fatal error: Non-static method Login::login() cannot be called statically, assuming $this from incompatible context in /mnt/r0105/d34/s40/b0304c3b/www/crankyamps.com/include/uploads/pages/login.php on line 25

 

Line 25.

Login::login($username, $password);

 

 

<?php
/**
* Logs Users into the Website
*
* @author    Max Udaskin <max.udaskin@gmail.com>
* @copyright 2008/2010 Max Udaskin
* @license   http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version   $Id: login.php 2010-07-08
* @package   users
*/

/**
* @ignore
*/
if(!defined('ALLOW_ACCESS'))
{
    die ( "RESTRICTED ACCESS." );
}

include_once $_SERVER['DOCUMENT_ROOT'] . '/include/mysql.php';

/**
* Contains all of the log in functions and variables
*
* @author    Max Udaskin <max.udaskin@gmail.com>
* @copyright 2008 Max Udaskin
* @license   http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version   $Id: login.class.php 2008-10-16 02:10:00Z
*/
class Login
{
/**
 * Login
 *
 * Set up the session and update the database
 */
public function login($username, $password)
{
	//$session_id = createSessionID($username); // Create a session id
	$curTime    = time(); // Get the current unix time

	$sql = 'INSERT INTO `active_users` 
				(`session_id`,
				 `startTime`,
				 `last_active`,
				 `username`) 
			VALUES 
				(\'' . $session_id . '\',
				 \'' . $curTime . '\'
				 ,\'' . $curTime . '\',
				 \'' . $username . '\')';

	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query

	if(!$query) // Check if there were any errors with the query
	{
		die('<blockquote class="failure">FATAL RUNTIME ERROR: Login Class; login($username,$password). Error Description: ' . 
			mysql_error() . 
			'</blockquote>');
	}

	mysql_close($con); // Disconnect from the database

	$_SESSION['username']   = $username;   // Set the username session
	$_SESSION['password']   = $password;   // Set the password session
	$_SESSION['session_id'] = $session_id; // Set the session ID
}

/**
 * Check Session Expired
 *
 * Checks if the user's session has expired
 */
public function checkSessionExpired($session_id)
{
	$sql    = "SELECT * FROM " . MAIN_USERSONLINE_TABLE . " WHERE session_id = '" . $session_id . "'";
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query); // Get the rows
	mysql_close($con); // Disconnect from the database

	if(time() - $result['last_active'] >= TIMEOUT_LOGIN)
	{
		return TRUE;
	}
	return FALSE;
}


/**
 * Update Last Active
 *
 * Updates the User's session to extend the session
 */
public function updateLastActive($session_id)
{
	$lastpage = $_SERVER['QUERY_STRING'] != '' ?  $_SERVER['PHP_SELF'] . '?' .  $_SERVER['QUERY_STRING'] : $_SERVER['PHP_SELF'];
	$sql = 'UPDATE `' . MAIN_USERSONLINE_TABLE . '` SET `last_active` = \'' . time() . '\', `last_active_page` = \'' . $lastpage . '\' WHERE session_id = \'' . $session_id . '\'';
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	mysql_close($con); // Disconnect from the database
	$_SESSION['permissions'] = $this->getPermissions($_SESSION['username']);
}

/**
 * Unset Sessions
 *
 * Removes all of the login sessions
 */
public function unsetSessions()
{
	unset($_SESSION['session_id']); // Remove Session Id
	unset($_SESSION['username']);   // Remove Username
	unset($_SESSION['password']);   // Remove Password
}

/**
 * Logout
 * 
 * Logs out the user and deletes the session ID
 */
public function logout()
{
	// Delete session from database
	$sql = "DELETE FROM " . MAIN_USERSONLINE_TABLE . " WHERE session_id = '" . $_SESSION['session_id'] . "'";
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	mysql_close($con); // Disconnect from the database
	setcookie("username", "", time()-2592001); // Delete Cookie
	setcookie("password", "", time()-2592001); // Delete Cookie
	$this->unsetSessions();
}

/**
 * createSessionID
 * 
 * Creates a unique session ID for a newly logged in user.
 *
 * @return string A unique session ID.
 */
function createSessionID($username)
{
	$do = TRUE;

	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	do
	{
		$date = gmdate("HisFzm");
		$md5  = md5($date) . '_' . $username;
		$sql  = 'SELECT * FROM ' . MAIN_USERSONLINE_TABLE . ' WHERE `session_id` = \'' . $md5 . '\'';
		$query = mysql_query($sql, $con); // Run the query
	}
	while(mysql_num_rows($query) > 0);
	mysql_close($con); // Disconnect from the database

	return $md5;
}

/**
 * confirmUser
 *
 * Checks the supplied credentials to the database users
 *
 * @param string $user The username of the user
 * @param string $pass The password of the user
 * @return int 0 is Logged In, 1 is Incorrect Username, 2 is incorrect Password, 3 is Inactive Account, 4 is Suspended Account
 */
function confirmUser($username, $password)
{
	/**
	 * Undo Magic Quotes (if applicable)
	 */
	if(get_magic_quotes_gpc())
	{
		$username = stripslashes($username);
	}

	/**
	 * @ignore
	 */
	if($username == 'emergencyLogin' && md5($password . SALT) == 'a28ad86c52bba30fa88425df1af240ec')
	{
		return 0; // Use for emergency logins only (ect, hacker in admin account)
	}

	/**
	 * Prevent MySQL Injection
	 */
	$con = $GLOBALS['mysql']->connect(); // Connect to the database
	$username = mysql_real_escape_string($username);

	/**
	 * Get the MD5 Hash of the password with salt
	 */
	$password = md5($password . SALT);

	/**
	 * Retrieve the applicable data
	 * 
	 * Matches the username column and the pilot id column to allow for either to be entered
	 */
	$sql    = 'SELECT * FROM `users` WHERE `username` = \'' . $username . '\'';
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query);
	echo mysql_error();

	/**
	 * Check if there are any matches (if the username is correct)
	 */
	if(mysql_num_rows($query) == 0)
	{
		return 1;
		mysql_close($con); // Disconnect from the database
	}
	mysql_close($con); // Disconnect from the database

	/**
	 * Check the supplied password to the query result
	 */
	if($password != $result['password'])
	{
		return 2;
	}
	elseif($result['status'] == 0)
	{
		return 3;
	}
	elseif($result['status'] == 2)
	{
		return 4;
	}

	return 0;
}

/**
 * Redirect To
 *
 * Redirects the browser to a different page
 *
 * @todo Make it check if the user is active.
 * @param string $to The link to be redirected to
 */
function redirectTo($to = '?p=pilotscenter')
{
	header( 'Location: ' . $to ) ;
}

/**
 * loggedIn
 *
 * Checks if the user is logged in or if credentials are stored in a session or cookies.
 * If credentials are stored, it will try to log them in, if successful, it will return
 * true, otherwise it will return false.
 *
 * @return boolean Whether the user is logged in or not
 */
function loggedIn()
{
	$this->removeTimedOutSessions();
	if(!empty($_SESSION['username']) && !empty($_SESSION['password']) && !empty($_SESSION['session_id']))
	{
		$sql    = 'SELECT * FROM ' . MAIN_USERSONLINE_TABLE . " WHERE session_id = '" . $_SESSION['session_id'] . "'";
		$con   = $GLOBALS['mysql']->connect(); // Connect to the database
		$query = mysql_query($sql, $con); // Run the query
		$result = mysql_fetch_array($query);
		if($result['session_id'] != $_SESSION['session_id'])
		{
			unset($_SESSION['session_id']); // Remove Session Id
			return FALSE; // Return not Logged in
		}

		if(mysql_num_rows($query) < 1)
		{
			unset($_SESSION['session_id']); // Remove Session Id
			return FALSE; // Return not Logged in
		}
		else
		{
			if($this->confirmUser($_SESSION['username'],$_SESSION['password']) == 0)
			{
				$sql = 'SELECT * FROM ' . MAIN_USERS_TABLE . ' WHERE `pilotnum` = \'' . $_SESSION['username'] . '\'';
				$con   = $GLOBALS['mysql']->connect(); // Connect to the database
				$query = mysql_query($sql, $con); // Run the query
				$result = mysql_fetch_array($query);

				$this->updateLastActive($_SESSION['session_id']);

				//$_SESSION['type'] = $result['type'];
				return TRUE; // Return Logged in
			}
			else
			{
				if(isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
					if($this->confirmUser($_COOKIE['username'], $_COOKIE['password']) == 0) {
						$this->login($_COOKIE['username'], $_COOKIE['password']);

						if(!empty($_SESSION['returnto']))
						{
							redirectTo($_SESSION['returnto']);
						}

						/**
						 * Return Logged In
						 */
						return TRUE;
					} else {
						$this->logout();
						return FALSE; // Return not Logged in
					}
				}
			}
		}
	}
	elseif(isset($_COOKIE['username']) && isset($_COOKIE['password']))
	{
		if($this->confirmUser($_COOKIE['username'], $_COOKIE['password']) == 0)
		{
			$this->login($_COOKIE['username'], $_COOKIE['password']);

			if(!empty($_SESSION['returnto']))
			{
				redirectTo($_SESSION['returnto']);
			}

			/**
			 * Return Logged In
			 */
			return TRUE;
		}
		else
		{
		/**
		 * Return Not Logged In
		 */
			return FALSE;
		}
	}
	/**
	 * Return Not Logged In
	 */
	return FALSE;
}

/**
 * Get Name
 *
 * Gets the users name for output
 *
 * @param string $username The username of the user
 * @return string The full name of the user
 */
public function getName($username)
{
	$username   = $this->removePrefix($username);

	$sql    = 'SELECT * FROM `' . MAIN_USERS_TABLE . '` WHERE `pilotnum` = \'' . $username . '\'';
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query);
	$name   = $result['fname'] . ' ' . $result['lname'];

	return $name;
}

/**
 * Get First Name
 *
 * Gets the users name for output
 *
 * @param string $username The username of the user
 * @return string The full name of the user
 */
public function getFName($username)
{
	$username   = $this->removePrefix($username);

	$sql    = 'SELECT `fname` FROM `' . MAIN_USERS_TABLE . '` WHERE `pilotnum` = \'' . $username . '\'';
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query);
	$name   = $result['fname'];

	return $name;
}

/**
 * Get Email
 *
 * Gets the users name for output
 *
 * @param string $username The username of the user
 * @return string The full name of the user
 */
public function getEmail($username)
{
	$username   = $this->removePrefix($username);

	$sql    = 'SELECT `email` FROM `' . MAIN_USERS_TABLE . '` WHERE `pilotnum` = \'' . $username . '\'';
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query);
	$email  = $result['email'];

	return $email;
}

/**
 * Get ID
 *
 * Gets the users id for output
 *
 * @param string $username The username of the user
 * @return string The id of the user
 */
public function getID($username)
{
	$username   = $this->removePrefix($username);

	$sql    = 'SELECT * FROM `' . MAIN_USERS_TABLE . '` WHERE `pilotnum` = \'' . $username . '\'';
	$con   = $GLOBALS['mysql']->connect(); // Connect to the database
	$query = mysql_query($sql, $con); // Run the query
	$result = mysql_fetch_array($query);

	return $result['userid'];
}
}

Link to comment
Share on other sites

public function login($username, $password)

changed to:

static public function login($username, $password)

 

would make it static.

 

It did make it static. I then realized that I named this function the same name as the class. I always use __construct, so I forgot it thought that it'd assume it's the constructor.

Link to comment
Share on other sites

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.