how to redirect a url? or how to redirect a page to another page? It is basic requirements of any website that was made in PHP or other languages. Today Redirection is an essential part of recent website. In redirection, you can actually divert your user to a particular page of your website.
For example, If user is working on your website and user wants to submit a comment or logged in/logged out on your website, then you should have to redirect to thank you page or user profile page if user wants to logged in.
PHP provides a simple and clean way to redirect visitors to another page.
Now in PHP, redirection is done by two types.
- Using header() function
- Using <meta http-equiv="refresh" content="0;url=http://www.google.com/" />
PHP code to redirect page:
- Create HTML code for sending information to server
<!DOCTYPE html>
<html>
<head>
<title>Title of page</title>
</head>
<body>
<form action="contact.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<textarea name="message"></textarea>
<input type="submit" name=”submit” value="submit">
</form>
</body>
</html>
- Create PHP code for giving response of HTML form
<?php
If(isset($_POST['submit'])) {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if($name && $email && $message) {
header('location: thankyou.html');
//or below types
echo '<meta http-equiv="refresh" content="0;url=http://www.google.com/" />'
}
}
}
?>
- HTML code for thank you page
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>Thank you!</p>
</body>
</html>
This is a simple PHP script for redirection one page to another page. Firstly we have sent input values like name, email and message by html form tag after that we used PHP script for checking and retrieving input fields value through POST method, which is only possible if we submit the form.