papaface Posted December 30, 2007 Share Posted December 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/ Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 ob_flush(); <<<<<< change ok...... Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425623 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 ob_flush(); <<<<<< change ok...... Changed But is it considered bad design to use it? Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425627 Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 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..... Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425636 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425645 Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 all looks grate but consider session instead of using the url..... Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425654 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 all looks grate but consider session instead of using the url..... What do you mean? Do you mean for the errors? Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425658 Share on other sites More sharing options...
trq Posted December 30, 2007 Share Posted December 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425660 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425661 Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 just use mod_rewrite to change the url off your current work... Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425666 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 just use mod_rewrite to change the url off your current work... Why though? They present no security issue and also I find sessions for such things pretty annoying as you have to keep clearing the session. Quote Link to comment https://forums.phpfreaks.com/topic/83666-is-this-a-good-way-of-working-around-an-issue/#findComment-425667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.