Jump to content

[SOLVED] Page name check & array


Andy17

Recommended Posts

Hey guys,

 

I have coded a login system and am therefore making a logout system (obviously). It works and when you logout, a session is set to 0 to show that you are not logged in anymore. However, if the user is on a page that requires him/her to be logged in, he/she will still be able to see the content before he/she either refreshes or navigates away. I do know a few ways to fix this, but it would be a little tricky.

 

I therefore thought of adding the login protected pages to an array, like this (just an example):

 

<?php

$loginpage_array[0] = "myorders.php";
$loginpage_array[1] = "profile.php";
$loginpage_array[2] = "settings.php";
$loginpage_array[3] = "submit.php";

?>

 

I would therefore need a script to check if the current page is the same as one of the pages in the array. I have the following function to get the current page:

 

<?php

function currentpage()

{

        return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

}

?>

 

So now I just need to somehow check if the current page name is in that array that I have made. I realize that there are several ways to do this, but my other ideas are too "tricky" and would require me to edit each login protected page. If you, however, have a better idea to do this, please let me know.

 

Thank you in advance!

Link to comment
https://forums.phpfreaks.com/topic/160566-solved-page-name-check-array/
Share on other sites

Thank you for your reply.

 

The thing is that it only works by clicking the logout button twice. I remember having this problem before, but I never solved it in a satisfying way (one that wasn't very complicated). This is what I have:

 

<?php

// Logout form
echo '<form name="logout" method="post"><input type="submit" value="Log out" name="logoutbutton" /></form>';

// If the logout button is pressed, then destroy the session
if (isset($_POST['logoutbutton']))

{

session_destroy();

}

if ($_SESSION['logstatus'] != 1)

{

header('Location: /');

}

?>

 

For some reason, using !isset did not work.

You do not need the two IF statements and the isset option, try the code below.

 

<?php

// Logout form
echo '<form name="logout" method="post"><input type="submit" value="Log out" name="logoutbutton" /></form>';

// If the logout button is pressed, then destroy the session
if ($_POST['logoutbutton']) {
   session_destroy();
   header('Location: /');
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.