Logout button not working

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1502 Views - Last Post: 05 April 2015 - 07:36 PM Rate Topic: -----

#1 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Logout button not working

Posted 31 March 2015 - 03:15 PM

My login button is not working when i click logout it should then run my logout script and echo you need to register or login

if i login with the correct details it redirects me from login.php to index.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 but i have gone over the code in the tutorial in these functions and they are correct i'm really lost on what to do now

DB.php
<?php
class 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
<?php
class 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;
	}
}



logout.php
<?php
require_once 'core/init.php';

$user = new User();
$user->logout();

Redirect::to('index.php');



Redirect.php

<?php
class 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();
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Logout button not working

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 31 March 2015 - 04:52 PM

What you're doing:
<?php
// Load in some stuff.
require_once 'core/init.php';

// Set $user to a new user.
$user = new User();
// Call the logout method for this new user.
$user->logout();

// Redirect to a new page where everything that just happened will be forgotten.
Redirect::to('index.php');


Was This Post Helpful? 0
  • +
  • -

#3 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Re: Logout button not working

Posted 31 March 2015 - 05:01 PM

i should have showed the code for index.php because it shows difrent things if your logged in or not

<?php
require_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>';
}


This post has been edited by Dormilich: 31 March 2015 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 31 March 2015 - 05:06 PM

It's not index.php, it's logout.php. The logout method is being called on a user that is not logged in. That method could work, but only if it destroys the current session.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 31 March 2015 - 05:13 PM

Another way to put it could be that your constructor is wrong. For example, both $user and $this->_sessionName will be null.

If you don't want to destroy the session you might just store the user as a session variable. Then all you need to do is set the user to logged out, save the session and redirect.
Was This Post Helpful? 0
  • +
  • -

#6 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Re: Logout button not working

Posted 01 April 2015 - 12:28 AM

View PostCTphpnwb, on 31 March 2015 - 05:13 PM, said:

Another way to put it could be that your constructor is wrong. For example, both $user and $this->_sessionName will be null.

If you don't want to destroy the session you might just store the user as a session variable. Then all you need to do is set the user to logged out, save the session and redirect.


my code is the same as the person in the tutorial in this video and his works fine

https://www.youtube....19&spfreload=10
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 01 April 2015 - 06:10 AM

So that means that you need to start thinking about what's going on. You can't ever hope to blindly follow some one else's code and have everything work. Small differences in code and system settings can make huge differences in results!

Let's look at your code again:
$user = new User(); // Who is this $user? Are they logged in? What will be the value of $user->_sessionName?
$user->logout(); // What will logout be acting on?


Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Logout button not working

Posted 01 April 2015 - 06:49 AM

CTphpnwb, the constructor for the User class will use the session data to load the user if constructed without an argument: https://www.youtube....1_yLAZzhWc#t=46
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 01 April 2015 - 07:09 AM

Yes, I believe that's the intention of this line:
        $this->_sessionName = Config::get('session/session_name');

but I don't think it's working, probably because the OP is missing something from a previous video.
Was This Post Helpful? 0
  • +
  • -

#10 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Re: Logout button not working

Posted 01 April 2015 - 03:20 PM

View PostCTphpnwb, on 01 April 2015 - 07:09 AM, said:

Yes, I believe that's the intention of this line:
        $this->_sessionName = Config::get('session/session_name');

but I don't think it's working, probably because the OP is missing something from a previous video.


everything works in the previous video (part 17)
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,614
  • Joined: 12-December 12

Re: Logout button not working

Posted 01 April 2015 - 06:07 PM

I assume that you have but I'll ask anyway: did you read the comments to the video(s) to see if anyone raised an issue?
Was This Post Helpful? 0
  • +
  • -

#12 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Re: Logout button not working

Posted 01 April 2015 - 06:59 PM

View Postandrewsw, on 01 April 2015 - 06:07 PM, said:

I assume that you have but I'll ask anyway: did you read the comments to the video(s) to see if anyone raised an issue?


yes only one person has and there has been no replys
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 01 April 2015 - 07:51 PM

View PostCripDawg, on 01 April 2015 - 05:20 PM, said:

View PostCTphpnwb, on 01 April 2015 - 07:09 AM, said:

Yes, I believe that's the intention of this line:
        $this->_sessionName = Config::get('session/session_name');

but I don't think it's working, probably because the OP is missing something from a previous video.


everything works in the previous video (part 17)

And do you have everything exactly as it is in all of the videos? Including the config settings?
Was This Post Helpful? 0
  • +
  • -

#14 CripDawg   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 15

Re: Logout button not working

Posted 01 April 2015 - 09:52 PM

yep up to part 18

This post has been edited by andrewsw: 04 April 2015 - 05:54 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Logout button not working

Posted 01 April 2015 - 10:19 PM

And you've tested parts 1 through 18? They each worked, but 19 doesn't?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3