If logged in display this.

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

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

If logged in display this.

Post by nickman013 »

Hello,

I have a very basic login with sessions setup.

To secure a page I put

Code: Select all

<?
session_start(); 
if (empty($_SESSION['log'])) 
header('Location:/pages/login.php');
include('/home/muot/public_html/pages/includes/regpage.php');
?>
that at the top of the page.

I want to put somthing into all of the pages, that if the user is logged in. It will display a logout link in the upper right hand corner.

if the user is not logged in, i dont want it to display it, I dont want it to do anytihng.


Thank You
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

<?php 
session_start();  
if (empty($_SESSION['log'])) {
header('Location:/pages/login.php');
exit();
} else {
//display whatever
}
include('/home/muot/public_html/pages/includes/regpage.php');
?>
By the way, it's bad practice to use short tags (<? and ?>) and to not use absolute URL's in Location:xxx.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I just want to add

Code: Select all

include("/root/usr/whatever/iflogged.php")
in every page.

and in the iflogged.php file I want it to do the following

1. If they are not logged in, do nothing.
2. If they are logged in display a logout link.


Thats it.

Thank You!
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

anyone???

thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I can't see where you actually asked a question so I will guess:

File iflogged.php:

Code: Select all

<?php
session_start(); 
if (isset($_SESSION['loggedin'])) {
    header('Location:/pages/login.php');
    exit();
}
?>
Pages:

Code: Select all

include 'iflogged.php';
// contents of page
Last edited by Christopher on Wed Feb 01, 2006 10:59 am, edited 1 time in total.
(#10850)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Now you can include this page...
iflogged.php

Code: Select all

<?php 
session_start();  
if (isset($_SESSION['loggedin']) === FALSE) { 
    header('Location:/pages/login.php'); 
}else{
?>
	<div style = 'text-align:right'>
		<a href = 'login.php?action=logout'>Log Out</a>
	<div>
<?php	
}
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Parse error: parse error, unexpected T_STRING in /home/muot/public_html/pages/includes/iflogged.php on line 3

Code: Select all

<?php
session_start();   
if (isset($_SESSION['loggedin']) === FALSE) {  echo "" 
}else{ 
?> 
<div style = 'text-align:right'> 
<a href = '/pages/login/logout.php'>Log Out</a> 
<div> 
<?    
} 
?>
Post Reply