Jump to content

[SOLVED] OOP System Issue


verheesj

Recommended Posts

Hey, I ran into a little issue, for some reason its not returning anything when Resource #5 is returned from the SQL by echo'ing out $this->query.

 

Here is my code, it should return a message if the username is found but the password isn't, and if the username isn't found then it should do the same as u can see below.

 

It just keeps redirecting to home.php when it should check

 

<?php

class system {
	private $mysql;
	private $username;
	private $password;
	private $query;
	private $main_url = 'http://example.com/';

	function __construct()
	{
		require_once("assets/classes/mysql.class.php");
		$this->mysql =& new mysql('localhost', '***', '***', '***');
	}

	public function login($username, $password)
	{
		$this->username = $this->mysql->prepare($username);
		$this->password = $this->mysql->prepare(md5($password));

		$this->query = $this->mysql->query("SELECT username FROM users WHERE username = '$this->username'");

		if(!$this->query) {
			echo "We did not find any users with the username you entered. Please try again.";
		}
		else
		{
			$this->query = $this->mysql->query("SELECT username, password FROM users WHERE username = '$this->username' AND password = '$this->password'");

			if(!$this->query)
			{
				return "We found the username, $this->username but the password you provided is wrong.";
			}
			else
			{
				$this->username = $_SESSION['username'];
				$this->password = $_SESSION['password'];

				return header("Location:" . $this->main_url . "home.php");
			}
		}
	}
}
$system =& new system;
?>

Link to comment
https://forums.phpfreaks.com/topic/134352-solved-oop-system-issue/
Share on other sites

<?php

class system {
	private $mysql;
	private $username;
	private $password;
	private $query;
	private $main_url = 'http://example.com/';

	function __construct()
	{
		require_once("assets/classes/mysql.class.php");
		$this->mysql =& new mysql('localhost', '***', '***', '***');
	}

	public function login($username, $password)
	{
		$this->username = $this->mysql->prepare($username);
		$this->password = $this->mysql->prepare(md5($password));

		$this->query = $this->mysql->query("SELECT username FROM users WHERE username = '$this->username'");

		if($this->mysql->num_rows($this->query ) < 1) {
			return "We did not find any users with the username you entered. Please try again.";
		}
		else
		{
			$query = $this->mysql->query("SELECT username, password FROM users WHERE username = '$this->username' AND password = '$this->password'");

			if($this->mysql->num_rows($this->query ) > 1)
			{
				return "We found the username, $this->username but the password you provided is wrong.";
			}
			else
			{
				$this->username = $_SESSION['username'];
				$this->password = $_SESSION['password'];

				return header("Location:" . $this->main_url . "home.php");
			}
		}
	}
}
$system =& new system;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.