Ninjakreborn Posted December 7, 2006 Share Posted December 7, 2006 A lot of the time's I use redirect quite a bit, to do little things, like if there not logged in, redirect them to the homepage. I am having troubles with it, and I also know why, I have a form, if it's successfully it redirects them back to the homepage, or so the client wants. I originally took the easy way out, they submit it, then message say insert successfully, click here to return home. Well he comes back and wants a redirect, but I don't know how.What do I have to do with this flush I have heard about, in order to be able to get a successfully redirection. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/ Share on other sites More sharing options...
trq Posted December 7, 2006 Share Posted December 7, 2006 Flush has nothing to do with redirecting. What exactly is your problem? Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136836 Share on other sites More sharing options...
Ninjakreborn Posted December 7, 2006 Author Share Posted December 7, 2006 It's giving me a header's already sent error.I know it's suppose to, because I have a bunch of validation before it, plus my config include.In lot's of posts I heard people say, if you need something to redirect, after something was outputted, use some combination of obflush and some other stuff, I just don't know how. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136920 Share on other sites More sharing options...
taith Posted December 7, 2006 Share Posted December 7, 2006 here... use this for every redirect... i'll save LOTS of time and agrivation...[code]<?function redirect($filename=".?op=main",$delay="0",$die="0"){ if((!headers_sent())&&($delay=="0")) header('Location: '.$filename); elseif($delay=="0"){ echo '<script type="text/javascript">'; echo 'window.location.href="'.$filename.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />'; echo '<noscript>'; }else echo '<meta http-equiv="refresh" content="'.$delay.';url='.$filename.'" />'; if($die=="0") exit;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136922 Share on other sites More sharing options...
trq Posted December 7, 2006 Share Posted December 7, 2006 Its output buffering. Have you searched the board for these [i]lot's of posts[/i]?[code]<?php ob_start(); // your code ob_end_flush();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136923 Share on other sites More sharing options...
redbullmarky Posted December 7, 2006 Share Posted December 7, 2006 if youre using a 'header: location', it will not be actioned until the script ends. so if you have anything after the redirect line that displays, you'll end up with the error. after your header line, stick an 'exit':[code]<?phpheader("Location: /somewhere.php");exit;?>[/code]in an ideal world, you should never do any validation like this after ANY form of content has been written to the screen. all your logic should be done and dusted before you even contemplate using echo or outputting HTML. this way, you can use the code I posted without too many issues and without even needing to use output buffering.so[code]<?php// ... all your validation stuff goes here// ... if any errors, redirect code goes here// ... include your header here// ... body here// ... footer here?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136925 Share on other sites More sharing options...
Ninjakreborn Posted December 7, 2006 Author Share Posted December 7, 2006 yes, but the format is like this.Yours[code]<?php// ... all your validation stuff goes here// ... if any errors, redirect code goes here// ... include your header here// ... body here// ... footer here?>[/code]What I thought[code]<?php// ... validation// ... record validation errors// ... display errors if there are any// ... if there are not then do database (file) work// ... if the work fails, return errors// ... if the work succeeds, he wants it to redirect back to the homepage so they can see // ... the results for himself.?>[/code]Or is there something specific I am overlooking. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136986 Share on other sites More sharing options...
redbullmarky Posted December 7, 2006 Share Posted December 7, 2006 if you want terrible messy code, then sure - put some display work, put some logic, put some more display work, a bit more logic, followed by a mix of everything else. without even seeing your code, i'm guessing it's gonna be quite unmaintainable. one thing i learned is whilst total seperation (MVC type thing) is not totally necessary, keeping some form of neat structure to your code goes a long way. even if i'm not using a template of some form, and not using MVC pattern, i'll always still make a point of doing ALL my logic first, and only then will i display the page.try it, and see the difference yourself. instead of displaying the validation errors where you have them, store them in a var/array UNTIL it gets to outputting your template/HTML stuff, and THEN display the errors. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-136995 Share on other sites More sharing options...
Ninjakreborn Posted December 7, 2006 Author Share Posted December 7, 2006 Yes that is how I do if, for example.at the top of the page of the same form, if it's single page I have something like indenting off some because it's in the forum though[code]<?phpif (isset($_POST['submit'])) { $errorhandler = ""; if ($_POST['firstname'] == "") { $errorhandler .= "First name was left blank.<br />"; } if ($_POST['lastname'] == "") { $errorhandler .= "Last name was left blank.<br />" } validate_email($_POST['email'], 1); if ($_POST['field1'] == "") { $errorhandler .= "Field 1 was left blank.<br />"; } // more validation of whatever type or kind is needed like above errorhandler($errorhandler, $url); // this is the function I use to display my error messages, if the // error handler is set, it displays it, as well as a url (if it's on a different page), to go back and fix // errors if ($errorhandler == "") { // here I do my mysql_real_escape_string or whatever other preperation before database entry // db variables (insert, query excetera) if (mysql_query($insert)) { header("location: redirect"); // example, I redirect here }else { echo "Problem inserting into database"; } }}?>[/code]probably a few errors there, just an example. That is how, i needed it to resurrect at that point, if it was successfully and if there were no errors. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-137044 Share on other sites More sharing options...
redbullmarky Posted December 7, 2006 Share Posted December 7, 2006 then in the case of an error, providing your code terminates and never reaches the line:[code] header("location: redirect"); // example, I redirect here[/code]you shouldnt have too many problems. as per my previous post, you need:[code]<?php header("location: redirect"); // example, I redirect here exit;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-137051 Share on other sites More sharing options...
Ninjakreborn Posted December 7, 2006 Author Share Posted December 7, 2006 Ok it worked, thanks, I will keep that in mind for later.Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-137142 Share on other sites More sharing options...
redbullmarky Posted December 7, 2006 Share Posted December 7, 2006 please note too that the code i showed is not a one off. i dont have a single header redirect that doesnt have an exit following it, and the only reason i ever use ob_start is for collecting a layout/part of a layout in a variable for use while templating.glad it worked. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-137154 Share on other sites More sharing options...
Ninjakreborn Posted December 7, 2006 Author Share Posted December 7, 2006 Hmm thanks that solves a problem I have ran into MANY times. THere were so many time's, I came up with an elaborate work around type system, as opposed to the way I wanted, this will help a lot later, thanks red. Quote Link to comment https://forums.phpfreaks.com/topic/29801-solved-thanks-redflush-then-redirect/#findComment-137161 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.