Hmm I'm not getting this,
I'm back to my original error and I no longer have the script working on my local server. Forgive me for posting ALL my code, but I suspect this is something fairly simple.
The error on my server is:
Code: Select all
Fatal error: Call to a member function query() on a non-object in /home/username/public_html/classes/Authenticate.php on line 27
Here's the
index.php page with the username and password box
Code: Select all
<?php
//require file contains links to the classes and the config file.
require_once("inc.php");
//check for a click on submit button
if (isset($_POST['submit']) || isset($_POST['submit_x']))
{
//both fields are required
if("" != $_POST['username'] && "" != $_POST['password']){
//create instance of authenticate class
$userLogin = new Authenticate($_POST['username'], $_POST['password']);
$userLogin->UserLogin();
if($userLogin->Success()){
//set the session values
$_SESSION['validUser'] = $userLogin->data[0]['username'];
$_SESSION['address'] = $userLogin->data[0]['address'];
$_SESSION['name'] = $userLogin->data[0]['name'];
//redirect user to their page, the address is taken from the database
$address = $userLogin->data[0]['address'];
header("location: " . "$address");
exit();
}else{
$message = "Incorrect Username or Password";
}
} else{
$message = "Both fields are required";
}
}
//?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container" id="container">
<div class = "contact_form" id = "contact_form">
<form name="contact" method="post" action="<?php echo($_SERVER['PHP_SELF']);?>">
<label for="username" id="username_label"><img src="images/username.png" width="82" height="14" alt="username" /></label>
<input type="text" name="username" id="username" class="text-input" />
<label for="passwrod" id="password_label"><img src="images/password.png" width="84" height="20" alt="password" /></label>
<input type="password" name="password" id="passwrod" value="" class="text-input" />
<br />
<input type="image" name="submit" class="button" id="submit_btn" src="images/transparent.gif"/>
</form>
<div class="error" id="error"><?php if($message) echo($message); ?></div>
</div>
</body>
</html>
Here's the new amended
Db.php class (many thanks for your help updating this!):
Code: Select all
<?php
//this class will establish a db connection WITHOUT using the mysqli driver
class DB{
public $connection;
public function __construct()
{
$this->connect();
}
public function Connect()
{
try{
//connect to db
$this->connection = mysql_connect(SERVER,DB_USER,DB_PWD); //these are functions, not classes like mysqli get rid of new
//is this actually selecting the database, I'm not convinced it works...
mysql_select_db(DB_NAME);
//check for connection error
if(mysql_error())
throw new Exception(mysql_error());
else
return $this->connection;
}catch(Exception $e){
echo("Connection failed, try again later" . $e->getMessage());
}
}
}
?>
Now the
Authenticate.php class:
Code: Select all
<?php
class Authenticate{
private $db;
public $data;
private $success = false;
private $username;
private $password;
public function __construct($username, $password)
{
$this->db = new DB();
$this->username = $username;
$this->password = $password;
}
private function Checking()
{
try{
//set sql query
$sql = "SELECT * FROM users WHERE username='{$this->username}' AND password='{$this->password}'";
//execute query and store the result
$result = $this->db->connection->query($sql);
//test
if(!$result)
throw new Exception($sql . $this->db->connection->error);
else{
return $result;
}
}catch(Exception $e){
echo("Login query failed: " . $e->getMessage());
}
}
//break the result into an array so the address can be carried
public function UserLogin()
{
//call the fetch function to get the query results
$userdetails = $this->Checking();
//loop through the set and create and array
while($row = $userdetails->fetch_assoc()){
$this->data[] = array('id'=>$row['id'],
'username' => $row['username'],
'password' => $row['password'],
'address' => $row['address'],
'name' => $row['name'],
);
}
if(!$this->data){}
else{
$this->data;
return $this->success = true;
}
}
public function Logout()
{
unset($_SESSION['validUser']);
session_destroy();
}
// return the current value of success property
public function Success()
{
return $this->success;
}
}
?>
The
config.php simply sets the database settings:
Code: Select all
<?php
// project configuration
define("SERVER","localhost");
define ("DB_USER","root");
define("DB_PWD","root");
define("DB_NAME","users");
session_start();
?>
And the
inc.php file:
Code: Select all
<?php
//load config parameters
require_once("config.php");
//application classes
require_once("classes/Authenticate.php");
require_once("classes/DB.php");
?>
The table is called "users" and contains id, username, password, address, name
THAT'S IT!
Does this post do anything for anyone? It would be great if you could help me out, as I mention, I'm fairly sure this is something simple. Any advance would be greatly appreciated!
-- wibblywobbly