0

Sorry guys if this a long winded post but I need to ask this the way it is. Well, I've tried this code on a couple of servers and it doesn't work properly!

I've been trying to change my PHP script from mysql to mysqli with more fails than success so far.

the registration form works fine, it will add the record to mysql database and it will send an email to the user as well without any errors. i got this on all my pages to make sure I am not getting any error:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?> 

The login form also works just fine and it will logs in the user to their account.

But here is the issue, well a couple of issues that I have been facing.

1- there is simple toplink at the top of the page which will show login and Register if the users are not logged in and it will show their username and logout links once they are logged in. this seems to have mind of its own as it works on one server and it doesn't work on another. by working i mean is that on one server it will stay on login and Register links even if the user is logged in.

2- there is a logout.php file which should log the users out and end the session. but this again has mind of its own. on the older PHP server it will work fine but if I refresh the page on the users account URL again, it will automatically logs the user back in. it doesn't matter which browser i try this in and how many times i clear the cache and cookies. it will still logs the user back in the account on page refresh.

also, the logout.php file doesn't work on a server with php version 5.3.21 and it will not log he user out of their account!!

This is the member.php code:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
session_start(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
    // Put stored session variables into local php variable
    $userid = $_SESSION['id'];
    $username = $_SESSION['username'];
    $toplinks = '<a href="member.php?id=' . $userid . '">' . $username . '</a> &bull; 
    <a href="member.php">Account</a> &bull; 
    <a href="logout.php">Log Out</a>';
} else {
    $toplinks = '<a href="join_form.php">Register</a> &bull; <a href="login.php">Login</a>';
}
?>
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = preg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
    echo "Missing Data to Run";
    exit();
}
//Connect to the database through our include 
include_once "config/connect.php";
// Query member data from the database and ready it for display
$sql = "SELECT * FROM members WHERE id='$id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$count = mysqli_num_rows($query);
if ($count > 1) {
    echo "There is no user with that id here.";
    exit(); 
}
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row["username"];
$_SESSION['username'] = $username;
$userid = $row["id"];
$_SESSION['id'] = $userid;
// Convert the sign up date to be more readable by humans
$signupdate = strftime("%b %d, %Y", strtotime($row['signupdate']));
}
?>

and this is the logout.php file:

<?php
session_start(); 
session_destroy();
if( isset($_SESSION['id'])){ 
header("location: index.php");
} else {
exit('<h2>Could not log you out, sorry the system encountered an error.</h2>');
} 
?> 
<html>
<body>
<?php echo "$msg"; ?><br>
<p><a href="index.php">Click here</a> to return to our home page </p>
</body>
</html>

any help would be appreciated.

roozi Far
  • 13
  • 3
  • The PHP manual has some good examples: http://php.net/manual/en/function.session-destroy.php – Jonast92 Mar 12 '13 at 16:05
  • PHP is the one that caused all this hassle for me. I have never ever understood a single code or word thats been said on PHP manual. and thats why I beg for information on google and forums and stackoverflow. – roozi Far Mar 12 '13 at 16:09
  • Possible duplicate of [why session\_destroy() not working](https://stackoverflow.com/questions/6472123/why-session-destroy-not-working) – Chirag Jain Jul 12 '18 at 13:43

2 Answers2

1

session_destroy() doesn't destroy session variables. You can use "$_SESSION = array();" if you want to do so. Best Regards

Corentin
  • 189
  • 1
  • 2
  • 13
  • Ah, ok... Maybe you can try the solution of this post : http://stackoverflow.com/questions/3989347/php-why-cant-i-get-rid-of-this-session-id-cookie – Corentin Mar 12 '13 at 16:10
  • Thanks, what do i have to use for $params["path"], $params["domain"], $params["secure"], $params["httponly"] ? or do i need to leave them as default? – roozi Far Mar 12 '13 at 16:26
  • You're welcome. "$params = session_get_cookie_params();" should be correct, you don't have to change it... – Corentin Mar 12 '13 at 16:29
  • Could you display the session variables after "$_SESSION = array();" ("var_dump($_SESSION);")? (Maybe the session is created again after.) – Corentin Mar 12 '13 at 16:38
  • This is what I get when I place the var_dump($_SESSION); after the "$_SESSION = array();" : array(0) { } Warning: Cannot modify header information - headers already sent by (output started at /logout.php:8) in /logout.php on line 16 – roozi Far Mar 12 '13 at 16:46
  • Ok, thanks. It means your session variables are correctly deleted. So the session variables are deleted at this moment. The header error is different. Anyway, if $_SESSION['id'] is not found, it shouldn't display an error about the log out but a success... Maybe you have to reverse your conditions : if $_SESSION['id'] exists, it should generate an error. Else, the page should be redirected. (I don't see when you want the "HTML" part of logout.php" to be displayed actually.) – Corentin Mar 12 '13 at 17:03
  • Thank you for the reply. I just need the HTML part to display after the session has been destroyed! I just cannot workout why nothing works! – roozi Far Mar 12 '13 at 17:40
  • So, this should be more correct (without "else"): if (isset($_SESSION['id'])) { exit('

    Could not log you out, sorry the system encountered an error.

    '); }
    – Corentin Mar 12 '13 at 17:43
  • that just return a white blank page! – roozi Far Mar 12 '13 at 17:51
-1

Try:

session_destroy();
session_write_close();
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
webbez
  • 1