Jump to content

OOP logout button and flash problem

CripDawg

my oop login and register script is working pretty well except for the Logout button on index.php im sure that the links a correct im pretty sure all the methods that are used are correct i back tracked to where my tutorial explains the methods and double checked my code  as far as i can see its correct but the button doesn't work i also the flash does not work when registration is complete will attach my entire project so far for reference.

 

thanks in advance for any help

index.phplogin.phplogout.phpregister.phpinit.phpConfig.phpDB.phpHash.phpInput.phpRedirect.phpSession.phpToken.phpUser.phpValidate.php

index.php

login.php

logout.php

register.php

init.php

Config.php

DB.php

Hash.php

Input.php

Redirect.php

Session.php

Token.php

User.php

Validate.php

Link to comment
Share on other sites

Link to post
Share on other sites

So are we supposed to download every file and just figure it out? What have you done so far to try to find the problem yourself?

Link to comment
Share on other sites

Link to post
Share on other sites

So are we supposed to download every file and just figure it out? What have you done so far to try to find the problem yourself?

should have done code blocks sorry 

 

I've checked the to methods delete,  to and login and made sure my href line has the correct file name  

 

Session.php

 

<?phpclass Session {public static function exists($name) {return (isset($_SESSION[$name])) ? true : false;} public static function put($name, $value) {return $_SESSION[$name] = $value;} public static function get($name) {return $_SESSION[$name];} public static function delete($name) {if(self::exists($_SESSION[$name])) {unset($_SESSION[$name]);}} public static function flash($name, $string = '') {if (self:: exists($name)) {$session = self::get($name);self::delete($name);return $session;} else {self::put($name, $string);}}}

 

logout.php

<?phprequire_once 'core/init.php'; $user = new User();$user->logout(); Redirect::to('index.php');

 

index.php

<?phprequire_once 'core/init.php'; if(Session::exists('home')) {'<p>' . Session::flash('home') . '</p>';} $user = new User();if($user->isLoggedIn()) {?><p>Hello <a href="#"><?php echo escape($user->data()->username); ?> </a></p> <ul><li><a href="logout.php">Log out</a></li></ul><?php} else {echo '<p>You need to <a href="login.php">Log In</a> or <a href="register.php">Register</a></p>';}
Link to comment
Share on other sites

Link to post
Share on other sites

Well since the problem concerns your logout script, let's start there.

 

Please write up an verbal explanation, line by line, of every piece of code that is executed when the script "logout.php" is run, excluding what is in your "init.php" file.

Link to comment
Share on other sites

Link to post
Share on other sites

Well since the problem concerns your logout script, let's start there.

 

Please write up an verbal explanation, line by line, of every piece of code that is executed when the script "logout.php" is run, excluding what is in your "init.php" file.

ok ill give it a shot  it will probably be wrong though haha im just copying a video series  

 

<?phprequire_once 'core/init.php'; $user = new User(); //not to sure what this line does $user->logout(); // this runs the logout method in User.php witch uses the delete function in DB.php to delete the session of the user   Redirect::to('index.php'); //redirect to index.php using the to function in Redirect witch will display you need to login or register because there is not a valid session anymore

 

DB.php

<?phpclass DB {private static $_instance = null;private $_pdo,$_query,  $_error = false,  $_results,   $_count = 0; private function __construct() {try {$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));} catch(PDOException $e) {die($e->getMessage());}} public static function getInstance() {if(!isset(self:_instance)) {self:_instance = new DB();}return self:_instance;} public function query($sql, $params = array()) {$this->_error = false;if($this->_query = $this->_pdo->prepare($sql)) {$x = 1; if(count($params)) {     foreach($params as $param) {      $this->_query->bindValue($x, $param);      $x++;      }    }  if($this->_query->execute()) {     $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);      $this->_count = $this->_query->rowCount(); } else {  $this->_error = true; }} return $this;} public function action($action, $table, $where = array()){if(count($where)=== 3) {$operators = array('=', '>', '<', '>=', '<='); $field  = $where[0];$operator  = $where[1];$value  = $where[2]; if(in_array($operator, $operators)) {$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; if(!$this->query($sql, array($value))->error()) {return $this;} }} return false; } public function get($table, $where) {return $this->action('SELECT *', $table, $where);} public function delete($table, $where) {return $this->action('DELETE', $table, $where);} public function insert($table, $fields = array()) {$keys = array_keys($fields);$values = '';$x = 1; foreach($fields as $field) {$values .= "?";if($x < count($fields)) {$values .= ', '; }$x++;} $sql = "INSERT INTO users (`" . implode('`, `', $keys) . "`) VALUES ({$values})"; if(!$this->query($sql, $fields)->error()) {return true; } return false;} public function update($table, $id, $fields) {$set = '';$x = 1; foreach($fields as $name => $value){$set .= "{$name} = ?";if($x < count($fields)){$set .= ', '; }$x++;} $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}"; if(!$this->query($sql, $fields)->error()) {return true; } } public function results() {return $this->_results;} public function first() {return $this->results()[0];} public function error() {return $this->_error;} public function count() {return $this->_count;} }

 

User.php

 

<?phpclass User {private $_db,$_data,$_sessionName,$_isLoggedIn; public function __construct($user = null) {$this->_db = DB::getInstance(); $this->_sessionName = Config::get('session/session_name'); if(!$user) {if(Session::exists($this->_sessionName)) {$user = Session::get($this->_sessionName); if($this->find($user)) {$this->_isLoggedIn = true;} else {//process logout}}} else {$this->find($user);}} public function create($fields = array()) {if (!$this->_db->insert('users', $fields)) {throw new Exception('there was a problem creating your account');}} public function find($user = null) {if($user) {$field = (is_numeric($user)) ? 'id' : 'username';$data = $this->_db->get('users', array($field, '=', $user)); if($data->count()) {$this->_data = $data->first();return true;}}} public function login($username = null, $password = null){$user = $this->find($username);  if($user) {if($this->data()->password === Hash::make($password, $this->data()->salt)) {Session::put($this->_sessionName, $this->data()->id);return true;}} return false;} public function logout() {Session::delete($this->_sessionName);} public function data() {return $this->_data;} public function isLoggedIn() {return $this->_isLoggedIn;}}

 

Redirect.php

 

<?phpclass Redirect {public static function to($location = null) {if($location) {if(is_numeric($location)) {switch($location) {case 404:header('HTTP/1.0 404 Not Found');include 'includes/errors/404.php';exit();break;}}header('Location: ' . $location);exit();}}}
Link to comment
Share on other sites

Link to post
Share on other sites

I assume you're following the tutorial in order to learn how to do it yourself? Start by finding out what that line that you aren't sure the purpose of does.

Link to comment
Share on other sites

Link to post
Share on other sites

I assume you're following the tutorial in order to learn how to do it yourself? Start by finding out what that line that you aren't sure the purpose of does.

it creates a new user object so that i can pull stuff from the user class

Link to comment
Share on other sites

Link to post
Share on other sites

it creates a new user object so that i can pull stuff from the user class

 

And what code is executed when a new user object is created?

Link to comment
Share on other sites

Link to post
Share on other sites

And what code is executed when a new user object is created?

i have no idea

Link to comment
Share on other sites

Link to post
Share on other sites

Does the term "constructor" ring any bells?

did a quick google......is that being able to call private variables outside of a class eg user and session variables?  

 

i know there is a constructor in user.php

 

if i login with the correct details it redirects me from login.php therefore the Redirecting is not the problem

 

if i delete the session cookie manually and refresh my browser it says you need to login or register as it should  and my href in index.php is correct then ether my logout function on user.php or my delete function in DB.php are incorrect an i on the right track with finding the issue i trust you already found it you just want me to find it myself

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×