PHP Session Destroy on Log Out Button

0 votes

I'm currently working on a website that requires users to log in with a username and password. The password security is handled at the folder level by a Realm within the operating system of the web server. This will have to do for the time being till we come up with a suitable PHP log-in mechanism.  I have three files (See code snippets at the bottom).

The method is: - On index.php, select the Log In button. - To access the authenticated index file, provide the login and password. To delete the cache and send the user back to the top-level index, click the logout button, which calls the logout.php code.

In essence, what I want to happen is that it doesn't "destroy the session" in the sense that you aren't requested to input the password again.

I'm a little lost on this one because of how little I know about PHP.

index.php (top-level file with login button)

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<a href="authenticate/index.php">Log In Btn</a>
</body>
</html>

authenticate/index.php (This folder is password protected - contains the index file with the log out button which links to the logout.php file)

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Log out</title>
</head>
<body>
<a href="logout.php">Log Out Btn</a>
</body>
</html>

authenticate/logout.php

<?php   
session_start(); //to ensure you are using same session
session_destroy(); //destroy the session
header("location:/index.php"); //to redirect back to "index.php" after logging out
exit();
?>

Can someone please help me with this?

Jul 26, 2022 in PHP by Kithuzzz
• 38,000 points
12,719 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hey there, I think I'm actually kind of having the same issue. I have a logout button (wich is an icon) and I want it to trigger my LogOut() function when clicked

I currently have this in my nav bar (the <a> inside the "if" are meant to be shown only if user is logged)

```php
#<?php
#if (isset($_SESSION['nom'])){
#   include 'logout.php';
#   echo"
#   <a href=\"/page2.php\">Missions</a>
#   <a class=\"prof\"href=\"profil.php\">
#      <img class='pP'src=\"".$_SESSION['photo']."\">
#   </a>
#   <button name='submit' class='logout' type='submit'><img src='images/icons/blurple_power.svg' width='30' height='30'>
#   </button>";
#   if(isset($_POST["submit"])) { LogOut(); }
#}    
#?>

 and this in a logout.php file :

```php

#<?php
#function LogOut (){
#    session_start();
#    session_destroy();
#    header("Location: index.php");
#    exit;
#}
#?>

So, I guess I'm missing something, because I'm a bit rusty since I haven't use php (or any web language) for several years ... But at this point, for some reason, my button isn't even clickable, nothing happens when hovered or clicked, it basically acts like it's just an image

answered Feb 11, 2023 by anonymous

edited Mar 5

Related Questions In PHP

0 votes
0 answers

How to call a PHP function on the click of a button

The two buttons on my functioncalling.php page ...READ MORE

Jul 24, 2022 in PHP by Kithuzzz
• 38,000 points
11,128 views
0 votes
2 answers

How can we create a session in PHP?

Hello, niroj. Here is my idea session_start(); $_SESSION['USERNAME'] ...READ MORE

answered Dec 7, 2020 in PHP by Famous
• 140 points
1,447 views
0 votes
1 answer

How to check if php session is already started or not?

Hello kartik, Use session_id(), it returns an empty string ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
6,003 views
0 votes
1 answer

How can we track Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Apr 29, 2020 in PHP by Niroj
• 82,840 points
1,499 views
0 votes
1 answer

How can I expire a PHP session after 50 minutes?

Hii @kartik, Use the session_set_cookie_params funciton to set the session. If necessary call this function ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
1,478 views
0 votes
1 answer

Invalid argument supplied for foreach() on line 25. Error in my php and mysql

Hello @viven , This should be a simple fix ...READ MORE

answered Aug 4, 2020 in PHP by Niroj
• 82,840 points
4,491 views
0 votes
0 answers

Using sessions & session variables in a PHP Login Script

I have created a login and register ...READ MORE

Jun 17, 2022 in PHP by narikkadan
• 63,600 points
730 views
0 votes
0 answers

PHP form - Registration Successful Message on New Page

After each entry has been verified, the ...READ MORE

Jul 24, 2022 in PHP by Kithuzzz
• 38,000 points
2,558 views
0 votes
0 answers

Pinterest login with PHP and cURL not working

I've been trying to use cURL to ...READ MORE

Aug 5, 2022 in PHP by Kithuzzz
• 38,000 points
2,071 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,854 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP