what is difference between header and include, where which one should be used
I am confused with two terms
header ("Location:homepage_php");
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 th开发者_运维技巧em and at what place out of these two one should be used.
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 <?php include('homepage.php'); ?>
.
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.
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
Header redirects the browser. Include tells php to include the contents of a file and execute it as PHP.
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.
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.
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.
the main difference in include and header is that the include
does not change the url but header
does. That means header
sends you (redirects you) to that page but include
fetch the page for you.
see this example:
this is from test.php which is including file from test2.php
this is from test1.php using header so it redirects me to test2.php
精彩评论