tags:

views:

144

answers:

7

Hello Everyone...

I am confused with two terms

1.header ("Location:homepage_php");

2.include("homepage.php");

I am guessing that header is used after checking password procedure and about include, you can use it anywhere. But i am not sure what is actual difference between them and at what place out of these two one should be used.

+2  A: 

1 tells PHP to send a Location header to the HTTP client, forcing a redirect to "homepage.php".

2 tells PHP to include "homepage.php" inline to execution of the current page.


As a note about your question, your confusion might be over the term "header". It is overloaded sometimes to refer to the top part of a page in reference to code separation. Code separation is a common practice where one puts PHP code/HTML used in multiple pages into a separate file, and then included in the top (header) of each page.

HTH,

-aj

AJ
well.. actually it forces it to relocate to "homepage_php" which is different from "homepage.php".. and its is also dependent on teh relative location of the current page that is being viewed.
Bingy
A: 

Header redirects the browser. Include tells php to include the contents of a file and execute it as PHP.

Mike Sherov
It doesn't tell it to execute specifically. You can include html code with the same include(); call.
Josh K
well yes, you can include html, but what gets included is executed as a PHP file, which includes html :-)
Mike Sherov
A: 

The first tells the browser to send a header to the browser to redirect to "homepage_php" (should be .?)

The second includes the file at the top. This is useful if you're using methods or classes stored in other files, or want the same content to appear on multiple pages.

Josh K
+2  A: 

Header forwards the user to a new page, so PHP reinitializes, it's like a HTML meta redirection, but faster.

Include just includes the file where you call it, and it executes it as PHP, just like if the code from homepage.php was written where you write include('homepage.php);.

Daan
It will execute it as php, simply because the interpreter is copying the file directly on the page. It doesn't have to be php, and can even be plain html.
Josh K
Yeah, if you don't start with `<?php ?>` tags in your included file, it will be interpreted as plain HTML. I forgot to mention that.
Daan
+4  A: 

The header function is used to send raw HTTP headers back to the client: PHP header function

<?php
header("HTTP/1.0 404 Not Found");
?>

The above (taken from the PHP documentation) sends a 404 header back to the client.

The include function is used to include files into the current PHP script (the same as require) PHP include function

vars.php

<?php
$color = 'green';
$fruit = 'apple';
?>

test.php

<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>

This example (again from the PHP documentation) includes the vars.php script in the test.php script and, after the include, allows the test.php script to access the variables declared in the vars.php script.

Matt Ellen
Nice examples. Would upvote you but I'm already at the limit.
AJ
It's great what you can find when you read the documentation! ;)
Matt Ellen
A: 

The first is used for redirecting users to a different page.

Second is mostly used in templating systems to use various pages in one page. eg header.php, and footer.php will be included in content.php.

Zaje
A: 

NOTE:

the header location will eb a location that is readable by the web browser... and not the directory structure. (which include does)

also the include method will not change the page that the browser is pointing at.

Bingy