PHP question...

He has: 4 posts

Joined: Apr 2007

I am not at all a php expert. I have been teaching myself a little here and there and have used it for form submissions and the like to stop spammers from junking up my email...mostly successful ( but apparently they are a talented bunch).
ANYWAY...
when you have a "users" area or "members" area of the site...an area inaccessable to the public without registration, or functions on the site that will not work for unregistered users...how does the page this resides on know if someone is registered or not? I am thinking surely there is some authetification code that goes on the top of the page somewhere looking for session or cookie information...is that correct? The log in creates a session and without that session the page reads the user as unregistered???
sorry if this seems like a stupid and simple question...

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Well, it depends on how you want to do it. Probably the simplest way to do it would be to use cookie authentication. I'm assuming that you know how to do the sign-in form since you said you know forms. What you need to do is set some session variable when the user logs in, like

<?php
$_SESSION
['active'] = true;
?>

...and be sure to call the session_start() at the beginning of every file.

Than, to check to see is the user is signed in or not, do something like

<?php
if (!$_SESSION['active'])
{
   
header('Location: sign/in/form.php');
    die();
}
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.