Jump to content

Logout button problem

CripDawg

my login and register pages are working fine just a logout button that isn't working I'm watching a oop tutorial on youtube and I'm unto this part 

 

I've tried some debugging that other forms have said i should do like the error log line but still no result sorry for attaching so much code but i feel that its necessary because their are a fair amount of methods that relate to the logout and some of them are on different files 

 

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'); error_log("session name: ".$this->_sessionName);  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;}}return false;} 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;}}

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>';}

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;} }

logout.php

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

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);}}}
Link to comment
Share on other sites

Link to post
Share on other sites

When you have a large amount of code like that, please wrap it all in a spoiler tag.

Link to comment
Share on other sites

Link to post
Share on other sites

When you have a large amount of code like that, please wrap it all in a spoiler tag.

how do i do that and do you have any idea how to fix my problem

Link to comment
Share on other sites

Link to post
Share on other sites

how do i do that and do you have any idea how to fix my problem

 

Just edit your post and add spoiler tags around the text like so

[spoiler]...[/spoiler]

And no, sorry. Unfortunately I'm not familiar with PHP.

Link to comment
Share on other sites

Link to post
Share on other sites

Just edit your post and add spoiler tags around the text like so

[spoiler]...[/spoiler]

And no, sorry. Unfortunately I'm not familiar with PHP.

could you maybe direct some people who might be able to help to this post please?

Link to comment
Share on other sites

Link to post
Share on other sites

logout button that isn't working

can you elaborate on that?

Link to comment
Share on other sites

Link to post
Share on other sites

This is probably the 20th thread you have posted asking for help with this "tutoral". It's clear that it is way above your current programming level. Parking lot this one for now and run through some basic intro to PHP stuff so that you can learn how the language and programming in general work before tackling a complex project.

Link to comment
Share on other sites

Link to post
Share on other sites

what is wrong with using 

<?php session_start();session_destory();?>

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

This is probably the 20th thread you have posted asking for help with this "tutoral". It's clear that it is way above your current programming level. Parking lot this one for now and run through some basic intro to PHP stuff so that you can learn how the language and programming in general work before tackling a complex project.

but I've gone so far with this and it seems to be exactly what i need so i would see it as a big waste of time if i was to start again

Link to comment
Share on other sites

Link to post
Share on other sites

can you elaborate on that?

when i click the logout button nothing happens where it should delete the session and then redirect to index.php witch should the echo 'you need to login or register' but nothing happens 

Link to comment
Share on other sites

Link to post
Share on other sites

but I've gone so far with this and it seems to be exactly what i need so i would see it as a big waste of time if i was to start again

Restarting a project is never a waste of time, especially when you're a beginner.

The things you learn from practicing will become incredibly useful when trying to build more complex projects.

 

Beside, the real waste of time is copy/pasting someone else's code without understanding any of it.

What are you gonna do after we fix your code and something else breaks ?

Make a new thread ? That's not how to program.

If you wanna learn PHP, head over to http://codecademy.com/and complete their PHP tutorial to get the hang of the basics.

Once that's done, the tutorials at http://www.tizag.com/phpT/from POST & GET to Cookies will teach you important stuff you need for your login page.

Then, http://www.sqlcourse.com/will teach you the basics of SQL then, head over to php.net to learn about the mysqli class,

then learn OOP then, learn what XSS, SQLI and encryption is and finally, maybe you'll come out with a decent login page at the end of it all.

 

The point is, learning to program isn't an easy task and takes years of practices.

If you wanna learn how to do it the "easy" way, follow my advice otherwise, Good luck.

 

Link to comment
Share on other sites

Link to post
Share on other sites

but I've gone so far with this and it seems to be exactly what i need so i would see it as a big waste of time if i was to start again

 

If you're doing this out of necessity you should be using a content management system, or at least a framework. No need to reinvent the wheel. Especially since this tutorial is fairly terrible and not meant to be used in production.

Link to comment
Share on other sites

Link to post
Share on other sites

<?phpif ($_GET['logout'] == true) {     session_destroy();}?>

Easy. url.com/?logout=true

I'm probably playing a game right now...

Link to comment
Share on other sites

Link to post
Share on other sites

<?phpif ($_GET['logout'] == true) {     session_destroy();}?>

Easy. url.com/?logout=true

 

where about in my code would i put that also i would prefer something that goes along with the code in the tutorial so i continue it because I'm almost finished it

Link to comment
Share on other sites

Link to post
Share on other sites

It's not the code that's bad, well it's not recommended to use sessions, but the code works. Whatever server it is in isn't granting permission to the application to delete the memory.

 

The code that actually removes the session is this function under user.php

public function logout() {Session::delete($this->_sessionName);}

You're calling that function from your Logout page. What that actually executes is in the Session.php file.

public static function delete($name) {if(self::exists($_SESSION[$name])) {unset($_SESSION[$name]);}}

Now what I'd say is that your logout isn't actually getting the userid or whatever it uses. I don't know what init.php So you should add this code below in logout.php.

print_r($user);
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

×