• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

php placement question


#1
I have created a log-in area and everything is technically sound on the front of logging in, viewing secure pages and then logging out.

At the very top of my secure pages I have the following PHP:

PHP:
<?php

session_start();

if ($_SESSION['username'])
{
	
	echo "You are logged in as: ".$_SESSION['username'];
	echo "<p>";
	echo "<a href='logout.php'>Click here to logout</a>";
	
}
else
	header ("location: index2.htm");

?>
<html>
<title>blah blah blah</title>
This displays "You are logged in as: MrPink" with "Click here to logout" at the top center of the page (obviously very undesirable as it pushes the content/divs down the page). So, could somebody help explain to me how I can place the PHP "You are logged in as : MrPink" where ever I want on the page?

(and yep, Reservoir Dogs fan)

Thanks
 

Corrosive

Moderator
Staff member
#2
Just break up the PHP into;

PHP:
<?php

session_start();

?>
at the top of the page.

Then place this part where you want the message to appear (in a div I'd imagine);

PHP:
<?php

if ($_SESSION['username'])
{
	
	echo "You are logged in as: ".$_SESSION['username'];
	echo "<p>";
	echo "<a href='logout.php'>Click here to logout</a>";
	
}
else
	header ("location: index2.htm");

?>
PHP can run and output anywhere you call it on the page.
 
#3
Thats brilliant and yet so simple, Thanks Corrosive.

I do have a problem though - Designs By Maverickâ„¢ - Web Design by David Barkham

Warning: Cannot modify header information - headers already sent by (output started at /home/designs6/public_html/membersarea.php:13) in /home/designs6/public_html/membersarea.php on line 205
and line 205 is -
header ("location: index2.htm");
Any ideas what's wrong here?
Thanks
 
#4
ah ha! I managed to sort it by adding :

This to the top of the entire document:
PHP:
<?php 

ob_start(); 

?>
and this at the end of the entire document:
PHP:
<?php 

ob_end_flush(); 

?>
simples, thanks.
 

Corrosive

Moderator
Staff member
#5
You could have disabled the warning using error handling but your solution is fine. Remember that error handling should be at a higher level during development and then it can be suppressed in the production environment :icon_biggrin:
 

Corrosive

Moderator
Staff member
#7
This is from the PHP manual website;

PHP:
<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>
and shows your options for error handling.

What you had was a warning only and should not have affected your application. Error handling is a debug tool really.

BTW, here is your new bible; PHP: Hypertext Preprocessor horrendous website to view and navigate but soooo many useful things it worth persevering with it :icon_smile: