Jump to content

Recommended Posts

Hello,

Please bare with me. I have been coding a login/register script as a starting point to my new project.

It was working fine until I added my header and footers to the system.

 

Login.php

<?php
ob_start();
include("includes/constants.php");
include(CreatePath("templates/header.php"));
if ($_POST['submit'])
{
    include(CreatePath("templates/checkloginform.php"));
}
else
{
include(CreatePath("templates/loginform.php"));
}
include(CreatePath("templates/footer.php"));
ob_get_contents();
?>

In templates/checkloginform.php it has header() functions. Now when I don't have the ob_start() and ob_get _contents() functions in, it triggers a "headers already sent" error.

 

Is using output buffering a good way of doing this, or does it just show bad script design?

 

Here is templates/checkloginform.php by the way:

<?php
include(CreatePath("includes/memberfunctions.php"));

if	($_mem->LoginCheckUsername($_POST['username']))
{
	if	($_mem->LoginCheckPassword($_POST['password']))
		{
            if	($_mem->Login())
            	{
			echo "You are logged in";
            	}
            else
            	{
            	header('Location: '.CreateURL("login.php?error=nli"));//not logged in (problem)
            	}
		}
	else
		{
		header('Location: '.CreateURL("login.php?error=ip"));//wrong password
		}
}
else
{
header('Location: '.CreateURL("login.php?error=unr"));//username not found
}
?>

 

And here is the working preview: http://papaface.com/qss/login.php

(ignore the template, its just temporary  :))

P.S Sorry about the formatting, pasting from PHP Expert Editor doesnt work correctly.

No it not bad programming using the ob_start or ob functions, there

a time and a place, For example if you can not find why the code keeps erroring

a header problam then you need help so use ob_start() and ob_flush()....

 

Now if your a person that use the ob function's  just becouse it there yes your

a right div, dont use ob_start() as a goal to all header problams...

 

ob_start() is not the end off the world or the begining as your

programming your notice what goes were and why .....

 

good luck all the best redarrow.....

 

code looks good carry on mate..........

 

WARNING :  THERE NO RIGHT'S OR WRONGS IN PROGRAMMING AS LONG

AS IT WORKS AND IS SECURE.....

Thanks for that. I have changed my coding so it is more logical.

Does this look better than the original?

<?php
include("includes/constants.php");
if ($_POST['submit'])
{
    //include(CreatePath("templates/checkloginform.php"));

include(CreatePath("includes/memberfunctions.php"));

if	($_mem->LoginCheckUsername($_POST['username']))
	{
		if	($_mem->LoginCheckPassword($_POST['password']))
			{
                         if	(!$_mem->Login())
            	               {
            	                 header('Location: '.CreateURL("login.php?error=nli"));//not logged in (problem)
            	               }
			}
		else
			{
			header('Location: '.CreateURL("login.php?error=ip"));//wrong password
			}
	}
else
	{
	header('Location: '.CreateURL("login.php?error=unr"));//username not found
	}

}
//Begin Output
include(CreatePath("templates/header.php"));
if	($_SESSION['loggedin'] == 1)
{
echo "You are logged in";
}
else
{
include(CreatePath("templates/loginform.php"));
}
include(CreatePath("templates/footer.php"));
//Output ends
?>

WARNING :  THERE NO RIGHT'S OR WRONGS IN PROGRAMMING AS LONG

AS IT WORKS AND IS SECURE.....

 

Pfff. I don't know where you got that idea from, but there are plenty of wrong ways of doing things.

 

While output buffering has its place, as a hack to fix what is otherwise an error however is not one of them.

WARNING :  THERE NO RIGHT'S OR WRONGS IN PROGRAMMING AS LONG

AS IT WORKS AND IS SECURE.....

 

Pfff. I don't know where you got that idea from, but there are plenty of wrong ways of doing things.

 

While output buffering has its place, as a hack to fix what is otherwise an error however is not one of them.

I presume therefore my latter version of doing the same thing is better than using ob?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.