Search the Community
Showing results for tags 'logout'.
-
Hi guys, I have the following logout code, which works just fine, as in it logs the user out and kills the session etc. However, there is one part that is not working and that is updating the database to change the is_logged_in to set to 0 rather than 1, which is set upon login. <?php session_start(); // Start the session // Include database connection require_once('includes/config.php'); // Check if user is logged in if (!empty($_SESSION['user_id'])) { logoutUser($conn, $_SESSION['user_id']); } else { redirectToLogin(); } // Close the database connection $conn->close(); /** * Logs out the user by updating their login status and destroying the session. * * @param mysqli $conn The database connection. * @param int $user_id The ID of the user to log out. */ function logoutUser($conn, $user_id) { // Prepare statement to update user login status $stmt = $conn->prepare("UPDATE users SET is_logged_in = ? WHERE user_id = ?"); $is_logged_in = '0'; // Set user status to logged out $stmt->bind_param("si", $is_logged_in, $user_id); // Execute the statement if ($stmt->execute()) { // Destroy the session session_unset(); session_destroy(); redirectToLogin(); } else { echo "Error updating user status: " . $stmt->error; } // Close the statement $stmt->close(); } /** * Redirects the user to the login page. */ function redirectToLogin() { header("Location: login.php"); exit(); } ?> If anyone can help that would be great. Thanks
- 10 replies
-
- php
- prepared statements
-
(and 2 more)
Tagged with:
-
Trying to figure out how, when I press my logout button, do I get my page to go back to it's original page without the ?variable=something at the end: header('Location: '.$_SERVER['PHP_SELF'].'?activationsent=1'); I own this site: http://www.golden-wand.com/phpfreaks.txt
-
Im having issues with logging out of a my website. I have successfully managed to log in. I am using sessions to do it and have been using session_unregister, which I believe is not depreciated. I have tried using unset but I am now getting the error Parse error: syntax error, unexpected '"authenticatedUser"' (T_CONSTANT_ENCAPSED_STRING) in C:\wamp\www\ShreddedNutrition\HTML\Logout.php on line 8 this is my log out code <?php session_start(); $appUsername = $_SESSION["authenticatedUser"]; $_SESSION["message"] = "You Have Logged out"; unset("authenticatedUser"); // Relocate back to the login page header("Location: Login.php"); //session_destroy(); ?> and this is to log in <?php include 'db.inc'; session_start(); $UserEmail =$_POST["EmailAddress"]; $UserPassword =$_POST["Password"]; $query = "SELECT * FROM members WHERE EmailAddress = '$UserEmail' AND password = '$UserPassword' "; $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!"); mysql_select_db($databaseName) or die ("Unable to select database!"); $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { $_SESSION["authenticatedUser"] = $UserEmail; // Relocate to the logged-in page header("Location: Index.php"); } else { $_SESSION["message"] = "Could not connect log in as $UserEmail " ; header("Location: Login.php"); } mysql_free_result($result); mysql_close($connection); ?>
-
i am using sessions. first on admin folder and second on user folder. in admin folder, i use $_SESSION['username']; in user folder, i use $_SESSION['username1']; now when i click on logout button in localhost/xxx/admin/logout.php i also logged out from localhost/xxx/logout.php. is there a way.. not to be logged out from user panel when click logout button in admin panel????
-
This problem is for real PHP freaks. I do not post in forums unless I really can't find a solution. I have been bugged by this for days now. Here it is. I have a page signup.php among other pages on the website. Once you logout from logout.php all session is destroyed and all cookies are deleted. you can visit the website as a visitor after logging out. I visited all pages to make sure I was really logged out. Everything is normal up till here. However, when I click on signup again which call signup.php, the old session becomes suddenly alive and you are logged in again as if you had never logged out. I have never faced this problem before and I have made many websites using PHP. I have tested and found that session is already there when the page loads and values are not formed by any variable or function manipulation. I am using hostgator shared hosting. Please guyz help me out. I am not sure what I am doing wrong here.