Lesson 15: PHP redirect header location - TOP10

Lesson 15: PHP redirect header location


We use header() function to redirect other pages on your site or external site. Syntax:  header($header, $replace, $http_response_code);
First argument $header is a header itself. We want to use header to redirect visitors so in our case we will use something like “Location: URL” where URL is obviously an url of a page to which you want to redirect visitor. It can be full url (http://some-random-url.com/pages/page.html) or relative URL (pages/page.html)
Boolean argument $replace indicates whatever previous similar headers should be replace or not. By default this value is set to true, so all similar headers are replaced.
The last argument is HTTP response code. While $replace argument maybe not so useful, $http_response_code might come handy, because it allows to select method of redirection. We will be interested in 3xx response codes as they are responsible for redirections.
 301 – Moved Permanently. This and all future requests should be directed to the given URI.  302 – Moved Temporarily. Usually browsers use it as 303 redirection  303 – See Other. Resource you are looking for can be found under different URL  307 – Temporary Redirect
It maybe a little bit confusing if you are new to this, so remember that only first argument in header function is required, others are optional.
Examples
Here are some valid PHP redirections:
header("Location: http://google.com");
header("Location: pages/page.php");
header("Location: http://google.com", true, 303);
header("Location: http://google.com", true);
Remember also that headers MUST be sent before any output is started. There are no exceptions from this rule, in fact you cannot even sent a blank line to the output, executing code like this:
text <?php header("Location: ditio.net"); ?> will throw warning “Cannot add/modify header information – headers already sent by…“. To avoid such situation you can use output buffering or just make sure that nothing was sent to the client.

MySQL and SQL

This section provides a tutorial introduction to MySQL by showing how to use the mysql client program to create and use a simple database. mysql (sometimes referred to as the “terminal monitor” or just “monitor”) is an interactive program that enables you to connect to a MySQL server, run queries, and view the results. mysql may also be used in batch mode: you place your queries in a file beforehand, then tell mysql to execute the contents of the file. Both ways of using mysql are covered here.
To see a list of options provided by mysql, invoke it with the --help option:  shell> mysql --help This chapter assumes that mysql is installed on your machine and that a MySQL server is available to which you can connect. If this is not true, contact your MySQL administrator. (If you are the administrator, you need to consult the relevant portions of this manual, such as MySQL Server Administration.)
Previous
Next Post »