PHP - Logout Not Re-directing...
Hey guys, me again. A few of you were helping me with my login script, which I did finally get working. I am having one small problem though. Upon clicking the logout link, it does not re-direct back to the login page, it just stays blank. I have pasted my code from my logout.php file for reference. Thanks in advance for your help!
Logout.php code <?php session_start(); session_destroy(); { header("location: login.php"); } exit(); ?> Similar TutorialsI am having problems understanding the reason for why the user has to click logout twice, here's the bulk of the code: <?php ini_set('display_errors',0); require_once 'header.html'; require_once 'db.functions.php'; require_once 'config.php'; $database = dbConnect($host, $username, $password, $database); // should output 1 or nothing at all! if($database == true) { // now connected? // carry on with logic of outputting the blog contents: $result = entries("SELECT * FROM entries"); printf("<table>"); while($row = mysql_fetch_array($result)) { printf(" <tr> <td>%s</td> <td>%s</td> </tr> <tr> <td colspan=\"2\">%s</td> </tr> ", $row[2], $row[4], $row[3]); } printf("</table>"); printf("\n\n"); session_name("jeremysmith_blog"); session_start(); if(array_key_exists('login',$_SESSION)) { if($_SESSION['login'] == 1) // change this to correspond with session on the login.php script { printf("<p>Welcome %s</p> <p>To logout, click <a href=\"index.php?action=logout\">here</a></p> ",$_SESSION['username']); } } else { printf("<p>You are not logged in, please click <a href=\"login.php\">here</a> to login.</p>"); } } else { printf("\n<p id=\"error\">Could not connect to database, please try again later.</p>"); } // init the logout script? if(array_key_exists('action',$_GET)) { if($_GET['action'] == 'logout') { // log user out of the system: unset($_SESSION['login']); unset($_SESSION['username']); session_destroy(); } } printf("\n"); // just for output format! require_once 'footer.html'; Why does the user have to click logout twice, have I missed anything? Any helps appreciated thanks. <?php session_start( ); //Create arrays as session variables to hold form variables and errors $_SESSION['track_errors'] = array(); $_SESSION['track_form_vars'] = array(); //Clear each of the form variables posted and assign to $_SESSION['formVars'] foreach($_POST as $varname => $value) { $_SESSION['track_form_vars'][$varname] = trim($value, 50); } // Validate the First Name if (empty($_SESSION['track_form_vars'] ["invoice_number"])) // First name cannot be a null string $_SESSION['track_errors']["invoice_number"] = "You must provide us your Invoice Number"; // Validate the First Name if (empty($_SESSION['track_form_vars'] ["track_order"])) // First name cannot be a null string $_SESSION['track_errors']["track_order"] = "You must provide us your Tracking Number"; if (count($_SESSION['track_errors'])) { // There are errors. Relocate back to the client form header("Location: track_order.php"); exit; } else { } ?> <?php include "sections/header.php";?> <?php include "sections/left.php";?> <!-- start content --> <div id="content"> <h2>Track Order</h2> <?php include "connections/dbconnect.php"; $invoice_number =$_POST["invoice_number"]; $track_order =$_POST["track_order"]; $query = "SELECT * FROM customers_orders WHERE invoice_no = '$invoice_number' AND tracking_no = '$track_order'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); print " <table class='tracking_table'> <tr> <td id='name' colspan='11'> Hello ".$row['fname']." ".$row['sname']." <p></p> </td> </tr> <tr> <td colspan='11'> Please find your personal tracking information below. <p></p> </td> </tr> <tr> <th class='text'> Invoice Number </th> <th class='dots'> : </th> <td colspan='9'> ".$row['invoice_no']." </td> </tr> <tr> <th class='text'> Date Ordered <th class='dots'> : </th> <td colspan='9'> ".$row['date_of_order']." </td> </tr> <tr> <th class='text'> Status <th class='dots'> : </th> <td colspan='9'> ".$row['status']." </td> </tr> <tr> <th class='text'> Date Expected <th class='dots'> : </th> <td colspan='9'> ".$row['expected_delivery_date']." </td> </tr> <tr> <th class='text'> Vehicle Make <th class='dots'> : </th> <td colspan='9'> ".$row['vehicle_make']." </td> </tr> <tr> <th class='text'> Vehicle Model <th class='dots'> : </th> <td colspan='9'> ".$row['vehicle_ordered']." </td> </tr> <tr> <th class='text'> Vehicle Colour <th class='dots'> : </th> <td colspan='9'> ".$row['vehicle_colour']." </td> </tr> <tr> <th class='text'> Vehicle Extras <th class='dots'> : </th> <td colspan='9'> ".$row['vehicle_extras']." </td> </tr> <tr> <td colspan='11'> Please use this to track your order for speed as it will be updated as and when it comes forward or moves out immediateley. <p></p> </td> </tr> </table> "; } else { header("Location: track_order.php"); exit; } ?> </div> <!-- end content --> <?php include "sections/right.php";?> <?php include "sections/footer.php";?> Any Idea why this isn't being re-directed to "track_order.php"? Sorry if it has been mentioned previously and can I be directed to the correct information. Hello I am having another minor issue where I am unable to spot where I am going wrong. The code for my logout button should redirect the user to index.php however I seem to be having an issue with the url. This is on my test server and I am using Code: [Select] $url = BASE_URL . 'index.php'; // Define the URL.however the button seems to be taking me to: Quote http://localhost/ICU/localhost/ICUindex.php An I am not quite sure why, the base_url should be localhost/ICU or localhost but the url given out is localhost/ICU/Localhost/ICUindex.php. why is it repeating itself? So where am I going wrong, is it in my understanding of the BASE_URL or something else? As always thank you in advance for any help. Hi all Just to let you know where I am up to currently. I have created a database for articles I want to display a a blog. I have created a blog.php page and am echoing out parts of the database. I have then created some dynamic links to display the full article on another page i've named newsarticle.php. My whole site uses includes. i.e. www.maplewebdesign.co.uk/index.php?page=home etc. I've created a sum domain - blog.maplewebdesign.co.uk but I want to re-direct this to my blog page which is www.maplewebdesign.co.uk/index.php?page=blog I hope this makes sense. I would really appriciate some help. My website is www.maplewebdesign.co.uk by the way. The blog page isn't linked yet, but the link is, www.maplewebdesign.co.uk/index.php?page=blog Thanks Adi Hi! I'm attempting to do this and feel that I'm fairly close. Would you take a look and throw me any bones? PHP Code: Code: [Select] <?php if (isset($_POST['ppassword']) && (stristr($string,'') === TRUE)) { switch($_POST['ppassword']) { case "Google": header("Location: http://www.google.com"); exit(); case "Yahoo": header("Location: http://www.yahoo.com"); exit(); case "Bing": header("Location: http://www.bing.com"); exit(); } } ?> The Form: Code: [Select] <form name="portal" id="portal" method="post" action=""> <input name="ppassword" type="text" id="ppassword" maxlength="25" onfocus="this.select()" onblur="this.value=!this.value?'company name':this.value;" value="company name" onclick="this.value=''" /> <input name="login" type="submit" id="login" value="Continue" /> </form> I appreciate any of your help. I feel close and this part Code: [Select] (stristr($string,'') feels like the missing piece of the puzzle. Hi, How logout is executed after 15 min. if does not hit or refresh any page. Code: [Select] <?php session_start(); $_SESSION['is_logged_out'] = 1; if($_SESSION['is_logged_out'] ==1) { echo("<SCRIPT LANGUAGE='JavaScript'>window.alert('You had been successfully signed out')</SCRIPT>"); session_unset(); session_destroy(); header("Location:chatframe.php"); } ?> I got this error Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\login\logout.php:6) in C:\xampp\htdocs\login\logout.php on line 9 which mistake im put? this is not working Code: [Select] <?php session_start(); $username = $_SESSION['username']; echo mysql_query("UPDATE members SET online='0' WHERE username='$username' AND online='1'"); session_destroy(); echo "You've been logged out. <a href='index.php'>Click here</a> to return."; ?> what im trying to do is change the online mysql data from 1 back to 0 as i want to show when a user is on and off line can anyone help how to logout using session in proper way ? can you give me the code When writing a logout script in PHP what steps should i take? Should i: 1.) unset all session vars like: unset($_SESSION["var1"]); unset($_SESSION["var2"]); or calls session_unset(); 2.) destroy session cookie like: setcookie(session_name(), "NULL", time()-60, $params['path'], $params['domain'], $params['secure'], isset($params['httponly'])); 3.) finally destroy the session data by calling session_destroy(); or maybe just only unset the session vars - that is making only the first step? I'm asking because I've seen many logout scripts that doesn't involve these 3 steps (i most cases doing only the first one as i stated) Thx. for help. Hi, im new here Need some help. I have a database which contains users, and for every user i have logged_in value which is 1 if user is logged in and 0 if not. I use this so that user can be logged in only once. When user clicks on logout link i set logged_in on 0. My problem is when browser closes logged_in for current user is not set to 0, and after that user can't login because my site thinks he is already logged in. is there some way I can call some function when browser exits or any other solution? please help Hi, Just wondered if anyone could help Ive been following this tutorial: http://net.tutsplus.com/tutorials/php/user-membership-with-php/ Ive got a simple membership system working now, but just wondering about the login / login links that i currently have. The login link is currently hard coded like so: Code: [Select] <ul id="menu"> <li id="active"><a href="index.html">Home</a></li> <li><a href="About.html">About</a></li> <li><a href="Contact.php">Contact</a></li> <li class="end"><a href="login.php">Login</a></li> </ul> and same for the logout: Code: [Select] <ul id="menu"> <li id="active"><a href="index.html">Home</a></li> <li><a href="About.html">About</a></li> <li><a href="Contact.php">Contact</a></li> <li class="end"><a href="logout.php">Logout</a></li> </ul> But the problem is, when i go to the about us page for example it will still display the login which really it should have logout. Could anyone offer some assistance please Hi there I have this problem with a button that should destroy sessions and then redirects to login page when clicked, pointing to this method: Code: [Select] public function logout(){ $this->logged_in = false; session_start(); session_unset(); session_destroy(); redirect_to("login.php"); } the function is called as bellow: Code: [Select] <form> <input id="logout" name="logout" type="submit" value="logout" onClick="<?php $session->logout(); ?> " /> </form> which is available on a page called index.php, which is the default page where you'll be directed to when logged in or just registered. unfortunately when I'm supposed to be to this page and before even clicking on the button, the function just does the work and redirects to the login page!!!.....any help please [/color][/size] Hi, I am having a bit of problem with my login/logout script. When user is logged in I want the script to show logout and if they are not login I want the script to show login. The problem is even when the user is logged in it says "you must be logged in Click here to login " here is the script Please help Code: [Select] <?php session_start(); $_SESSION['username'] = $_POST['username']; if ($_SESSION['username']) echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>" ; else die("you must be logged in <a href='Login.php'>Click here to login</a>"); ?> what did I do wrong in this script ? Thanks Hello, I'm currently storing a timestamp of the users last activity in my database in which if that hasn't been updated for 15 minutes (for testing doing 2 minutes) I want it to log the user out. I have been trying different things but they all seem to log me out even though they shouldn't be. Example of something I've tried $Online = time() - 120; if ($CheckOnline['lastaction'] < $Online){ header("Location: Logout.php"); session_destroy(); } Am I going at this the wrong way.? If I do $Online < $CheckOnline['lastaction'] it keeps me logged in but never logs me out.
Any help would be brillaint.
Thank you! For some reason my logout is not working properly. I searched other forum posts and read some tutorials but I still have an issue. Do this to create session: Code: [Select] $_SESSION['username'] = $row['username']; setcookie('username', $row['username'], time() + (60 * 60 * 24 * 5)); Do this to log out the user Code: [Select] session_start(); session_unset(); $_SESSION = array(); setcookie(session_name(), '', time() - 3600); session_destroy(); setcookie('username', '', time() - 3600); Above all else I am trying to end the $_SESSION['username'] Thank you all I based this off some other pages read, and think I'm doing this wrong or it's just not connecting. Code: [Select] Here's the database table: CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(30) NOT NULL UNIQUE, password VARCHAR(64) NOT NULL, salt VARCHAR(3) NOT NULL, PRIMARY KEY(id) ); Ando far I have index.php with my login form <form name="login" action="login.php" method="post"> Username: <input type="text" name="username" /> Password: <input type="password" name="password" /> <input type="submit" value="Login" /> </form><br />Would you like to <a href="register.php">register?</a></center> Then I have my actual login on login.php (header.php includes website's main image as well as session_start(): <?php include('header.php'); $username = $_POST['username']; $password = $_POST['password']; //connect to the database here $username = mysql_real_escape_string($username); $query = "SELECT password, salt FROM users WHERE username = '$username';"; $result = mysql_query($query); if(mysql_num_rows($result) < 1) //no such user exists { header('Location: login.php'); die(); } $userData = mysql_fetch_array($result, MYSQL_ASSOC); $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) ); if($hash != $userData['password']) //incorrect password { header('Location: login_form.php'); die(); } else { validateUser(); //sets the session data for this user } //redirect to another page or display "login success" message ?> then I have my register php on register.php: <?php include('header.php'); //retrieve our data from POST $username = $_POST['username']; $pass1 = $_POST['pass1']; $pass2 = $_POST['pass2']; if($pass1 != $pass2) header('Location: register_form.php'); if(strlen($username) > 30) header('Location: register_form.php'); $hash = hash('sha256', $pass1); function createSalt() { $string = md5(uniqid(rand(), true)); return substr($string, 0, 3); } $salt = createSalt(); $hash = hash('sha256', $salt . $hash); $dbhost = 'localhost'; $dbname = 'mygame'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname, $conn); //sanitize username $username = mysql_real_escape_string($username); $query = "INSERT INTO users ( username, password, salt ) VALUES ( '$username' , '$hash' , '$salt' );"; mysql_query($query); mysql_close(); header('Location: login.php'); ?> and lastly the register form: <center><form name="register" action="register.php" method="post"> Username: <input type="text" name="username" maxlength="30" /> Password: <input type="password" name="pass1" /> Password Again: <input type="password" name="pass2" /> <input type="submit" value="Register" /> </form></center> I am getting the errors: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/testing/login.php on line 13 Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/testing/config.php:1) in /Applications/XAMPP/xamppfiles/htdocs/testing/login.php on line 15 Could someone explain why this is happening? I was wondering if there is away to set a logout message when you are using sessions this is my code <?php session_start(); if($_SESSION[auth] = "yes") { session_unset(); session_destroy(); header( "Location: index.php" ); exit(); } else { if ($_SESSION[auth] != "yes") { header( "Location: index.php" ); $_SESSION['error'] = "please login!"; exit(); } } ?> is it possible to update data in mysql when a user clicks logout? I had set this value into the super global $_SESSION in the first file, file_1.php: <?php //there is some code here $_SESSION['salesorder'] = 'are---io'; //remaining code, includes a form. ?> When the submit button is clicked, the form is submitted to file_2.php <?php //some code over here if (isset($_POST['sales']) && $_POST['sales'] != ""){ $sales = sanitize_input(trim($_POST['sales'])); $_SESSION['salesorder'] = $sales; $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $extra = 'processSO_helper_sd.php'; header("Location: http://$host$uri/$extra?so=".$sales); exit(); } //Begin display include 'inc_fn_header_and_menu.php'; //some code over here ?> which will redirect to processSO_helper_sd.php <?php // Initialize session session_start(); $SD_ID = $_SESSION['salesorder']; $result = $db->query($sql); $row = $result->fetch_assoc(); $rowcount =$row['row_count'];*/ //print_r($_SESSION);//* $result = $tmonedb->query("SELECT COUNT(*) FROM document WHERE SD_ID = $SD_ID")->fetch_array(); $rowcount = $result[0]; //remaining code ?> My problem is that I cannot access the $_SESSION['salesorder'] in the last file processSO_helper_sd.php although I have set it twice previously. I could not add session_start(); in the file_2.php because that was already added in 'inc_fn_header_and_menu.php'; Can someone help me with this? Thank you. Please tell me if you need any additional information. |