Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/116705-newbie-what-does-header-do/
Share on other sites

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.

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

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.

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.

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.