In this tutorial we will learn how to redirect to another page in PHP. Moslty, we see when user login or register then site make user redirect to another page. Today, we will learn how to do the redirection once user fill the form.
How Redirection Works in PHP
In PHP when you want to redirect, you need to use header() function. The header function allows you to send a raw HTTP location header.
Let’s know how header() function works
header("Location: https://www.yoursite.com/yourpage.php");
In this header function, we are using “Location” option. All you just need to put url of page you want to redirect. When the above code will run, it will redirect you to https://www.yoursite.com/yourpage.php and will be 302 redirection which means it temporary redirection. If you want to make it permanent or 301 redirect you can add 301 in header function parameter.
Check below code if you want permanent url.
header("Location: http://www.yoursite.com/yourpage.php", TRUE, 301);
Between, you should add exit or die function after the header function like this
header("Location: http://www.yoursite.com/yourpage.php", TRUE, 301);
exit();
Step By Step Guide On How To Redirect To Another Page In PHP
Now we will create a registration form in php and then redirect to another page. Once user register.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>home page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main">
<div class="inner">
<div class="welcome">
Student's Registration Form
</div>
<div class="registration_options">
<form action="" method="post">
<div class="rleft_body">
<label for="">
Enter your name here
</label>
<br>
<label for="">
Enter your email here
</label>
<br>
<label for="">
Enter Student's phone number
</label>
<br>
</div>
<div class="rright_body">
<input type="text" name="name" id="">
<br><br>
<input type="email" name="email" id="">
<br><br>
<input type="tel" name="nmbr" id="">
<br><br>
</div>
<div class="registration_submit">
<input type="submit" value="Submit" id="save" name="button">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Once you’ve create the form in HTML. you need to add php code.
How html code works
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.\
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used to add webpage title. Always remember title tag will be under <head> tag
- Thirdly, <body> tag is used to define the webpage body. All the contents to show on website are written here.
- Here, we create a simple registration page with name, email, phone number, etc.
- Now, after that form we will create the PHP code and there we will redirect only if datainsert into the table.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['nmbr'];
// your code to insert fields in database
$insertid=$yourlastinsertid;
if ($insertid) {
header("location:yourpage.html");
exit();
} else {
echo "Something is wrong";
}
}
?>
How PHP code works
- In this PHP code, we use isset() function which means the header redirection work only if PHP post the data via submit button, else it will not work.
- Then we are storing post data to different variables like $name store $_POST[‘name’] and $email store $_POST[’email’].
- These variables are useful to send data to database as we don’t need to write $_POST[‘name’] as we will just call $name variable.
- I’ve added commented code //your code will there, So that you can add insert code into database. I did this, So that you can put your table name and database.
- Once you’ve add the insert code into the above code, then you need to put last insert id in $insertid variable. This will help to run further code, as I’ve added condition that header will only work if $insertid variable is not empty then header function will work and redirect to yourpage.html, else you’ll get the message that something went wrong.
- Lastly, you can show that message in register page if something is wrong while inserting to database else you are just simple redirecting to new page.
Conclusion
You can just copy the above code and paste. You just need to play with the code. If you want just a single line code you can copy the below code.
header("Location: http://www.yoursite.com/yourpage.php", TRUE, 301);