PHP Login System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

DarkAngel
Forum Newbie
Posts: 12
Joined: Sun May 23, 2004 11:01 am

Post by DarkAngel »

Ok last question, what would I put in other php files that only allow the file to be viewed if a user is logged in?
DarkAngel
Forum Newbie
Posts: 12
Joined: Sun May 23, 2004 11:01 am

Post by DarkAngel »

Ok I tried:

Code: Select all

<?php
<?php
if($_SESSION['loggedin'])
{
print "
what logged in members would see
";
} else {
print "
You do not have access to this file. Please log in.
";
}

?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i dont know how you set it up

but put after the log-in (if indeed the name n password matched)

$_SESSION['auth'] = true;

then use:

Code: Select all

<?php
if ( !$_SESSION['auth'] ) { 
   // what not logged in users would see
} 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Correct me if I'm wrong but is this correct:

Code: Select all

<?php
//Only works with register_globals being 'ON'.

unset( $_SESSION['variable'] );

?>
For the sake of mobility of the script,
use

Code: Select all

<?php

$_SESSION['variable'] = array();

?>
And I would also recommended avoiding statements like

if ($var) // do something

and get in the habbit of doing if (isset($var)) // do something

Once again for the sake of mobility and readability of the script.
Hense if you move to a server with register globals OFF.
User avatar
mendingo
Forum Commoner
Posts: 28
Joined: Sun May 23, 2004 1:27 pm

Post by mendingo »

Phenom wrote:Correct me if I'm wrong but is this correct:

Code: Select all

<?php
//Only works with register_globals being 'ON'.

unset( $_SESSION['variable'] );

?>
unset($_SESSION['variable']) works fine without register_globals being on.
If register_globals were on, then you could use unset($variable), but the former works universally.

Your second point is valid, unless you have explicity set $var as false before doing your check to make it true.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Tutorial on using Sessions: viewtopic.php?t=6521
DarkAngel
Forum Newbie
Posts: 12
Joined: Sun May 23, 2004 11:01 am

Post by DarkAngel »

Ok my page is set out like this:

їcode]
|-----------------------------------------------------|
| Index page |
| |
| |---------------------------------------| Nav bar |
| | Iframe | |
| | | |
| | | |
| | | |
| | | |
| |---------------------------------------| |
| Login/Out Form|
|-----------------------------------------------------|
ї/code]
(the Iframe is contained in the index page as is the login/out form and the nav bar, but obviousley the iframe points to a seperate php file)

Now all the session code is in the index page, but when a user clicks a link (something like ?page=pie) in the nav bar it sets the iframe's src to whatever page= (and .php). Now, what do i have to put into pie.php to make it so it displays x if the user is logged in and y when the user isnt logged in?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

hmm

did u even read the link provided by our good admin jason?

read it!!!!!!

:evil:
Post Reply