OM2 Posted July 26, 2008 Share Posted July 26, 2008 I'e been going through some PHP code. I'm a little confused what header() is for? I've looked up php.net and it says: header — Send a raw HTTP header Erm: that measn nothing to me! Mostly, when I've seen header, I've seen it be used like: header("Location: http://www.example.com/"); With the above line, is it just an escape clause in a script anywhere to take the user to somewhere else? Thanks! OM Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/ Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 you can use header() for many things when creating an image you can tell it the page is jpeg not html you can use it for custom error handlers to sent 404 messages there is also a way to use it to create a login screen the same as a htaccess file would. you can use it to tell the browser not to cache the page so if it changes next time the browser will always download a new one to can use it to force a browser to download a file and all sorts of other things Scott. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600072 Share on other sites More sharing options...
ignace Posted July 26, 2008 Share Posted July 26, 2008 Headers are sent from the server to the client, so both know what they received and how they should handle it, for example when you typed in phpfreaks.com you made a request, to get the contents from the server, the server made a response something like this: Content-Type: text/html; charset=ISO-8859-1 <html> <head> <title>b.. </head> </html> to get a better understanding i suggest you go to gmail login into your gmail account open an e-mail and press the down arrow on the right of the e-mail, you will get a dropdown and in that you select "display original" that way you can see what server and client sent to each other, the server did not send you this graphical user interface it was rendered this way by your browser you're browser received this long text and passed it through his render engine Gecko if u are using Opera or firefox, that way you get what we call a user interface, which makes it possible for you to communicate with the server Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600089 Share on other sites More sharing options...
OM2 Posted July 26, 2008 Author Share Posted July 26, 2008 hmmm interesting. let's say i have some code in php and that checks to see if a user is logged in or not. if the user is not logged in, then they should be taken to another page. should i use a header call to do this? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600130 Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 hmmm interesting. let's say i have some code in php and that checks to see if a user is logged in or not. if the user is not logged in, then they should be taken to another page. should i use a header call to do this? thanks. Yep, you could do a header("Location: http://www.example.com/"); to redirect them. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600133 Share on other sites More sharing options...
OM2 Posted July 26, 2008 Author Share Posted July 26, 2008 thanks for confirming. is that the only way? if not: is that the best way? so i would have somehing like: if(condition) { // welcome in, have a nice day} else {header("Location: http://www.goaway.com/");} thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600145 Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 Yeah that should work mate. It's not the only way though, you could use meta tags or .htaccess. There was a topic about it yesterday I think. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600151 Share on other sites More sharing options...
OM2 Posted July 26, 2008 Author Share Posted July 26, 2008 thanks. i need to read up and learn more. for now: using header to do redirects forme will work great. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600155 Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 Just one more thing I forgot about mate, if you have content before you make the header() call, eg: echo 'Go away.'; header("Location: http://www.goaway.com/"); It won't work. To fix this, either take away any content before it or put ob_start(); directly under your opening PHP tag, and ob_end_flush(); directly above your end PHP tag: <?php ob_start(); echo 'Go away.'; header("Location: http://www.goaway.com/"); ob_end_flush(); ?> This is only necessary if there's content before the header() call though. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600156 Share on other sites More sharing options...
OM2 Posted July 26, 2008 Author Share Posted July 26, 2008 thanks for that. i actually did read about that on php.net. ur example though makes it much more clearer to understand. Quote Link to comment https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/#findComment-600161 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.