Hello all. I’m currently experimenting with PHP and I am tweaking around with lines of code from a registration system that I have downloaded. I’m in the process of editing the way it checks if the user is authenticated and what to do if he/she is not. Before I start this off I’ll link you guys to the reg-system which can be found, here http://www.olate.co.uk/articles/185.
The original form of the system uses the following to hide pages from non-authenticated users:
[php] <?php
include ‘init.php’;
if (!is_authed()) {
die (‘You are not permitted to view this page, click here to go back.’);
}
// Restricted articles code here
?>
[/php]
Instead of having to paste the if-statement on every page i want to be hidden, i have created my own function:
[php]
function page_protect() {
// Check if the encrypted username is the same
// as the unencrypted one, if it is, it hasn’t been changed
if (isset($_SESSION[‘username’]) && (md5($_SESSION[‘username’]) == $_SESSION[‘encrypted_name’])) {
return true;
}
else {
die(‘You are not allowed to see this page!’);
}
}
[/php]
I can just call this function at the top of every page I want hidden instead of pasting the if-statement (I based the function off of a more complicated login script… since I chose a very primitive one so i can get familiar with PHP better).
The authentication function works fine. I made a sample page to test it out on and if the user is not logged in it will not display the content of the page and instead will display the error. Unfortunately, neither will it display the rest of my page after the content section. The sample website I created has two columns… my content is in my first column and my navigation is in my second column. Since my function has die() the navigation column is cut off. I have tried with exit() as well but since both are similar functions it produces the same result.
One last thing that may help you is that my sample website is displayed using the $_GET[‘page’] and switch($page) gateway. I have always used this to layout websites as it made it a lot easier to manage the content. I know now that it isn’t a very good way to layout websites when they include reg-systems and the such because of all the header output errors I get. Anyways, here is the sample hidden page called articles.php:
[php]
_Main Content
You should not be able to see this page if you are not logged in.
[/php] According to my function, if the user is not logged in then it will exit() and not display the content underneath it. It does it but it also does not display the rest of my page which is located on my "index.php"... which contains the navigation column. Is there a way I can have my function not display the content underneath it.. only on the page it is called? Or is the only solution to layout my website another way using includes and requires to avoid this?Sorry for the long post just wanted to be as specific as possible. If you need me to post anything else… such as my index.php file just let me know. Thanks in advance.