How to make a redirect in PHP?

2018-12-30 22:07发布

Is it possible to redirect a user to a different page through the use of PHP?

Say the user goes to www.example.com/page.php and I want to redirect them to www.example.com/index.php, how would I do so without the use of a meta refresh? Possible?

This could even protect my pages from unauthorized users.

标签: php redirect
28条回答
爱死公子算了
2楼-- · 2018-12-30 22:31

Yes it's possible to use PHP, we will redirect to another page, try this one:

<?php
header("location:./");//redirect to index file
header("location:index.php");//redirect to index file
header("location:example.php");
?>
查看更多
临风纵饮
3楼-- · 2018-12-30 22:31

There are multiple ways of doing this, but if you’d prefer php, I’d recommend the use of the header() function.

Basically

$your_target_url = “www.example.com/index.php”;
header(“Location : $your_target_url”);
exit();

If you want to kick it up a notch, it’s best to use it in functions, that way, you are able to add authentications and other checking elemnts in it.

Let’s try with by checking the user’s level.

So,suppose you have stored the user’s authority level in a session called u_auth.

In the function.php

<?php

function authRedirect($get_auth_level, $required_level, $if_fail_link = “www.example.com/index.php”){
    if($get_auth_level != $required_level){
        header(location : $if_fail_link);
        return false;
        exit();
    }else{
        return true;
    }
 }

 . . . 

You’ll then call the function for every page that you want to authenticate.

Like in page.php or any other page.

<?php

// page.php

require “function.php”

authRedirect($_SESSION[‘u_auth’], 5);  // redirects to www.example.com/index.php if the user isn’t auth level 5
authRedirect($_SESSION[‘u_auth’], 4);  // redirects to www.example.com/index.php if the user isn’t auth level 4
authRedirect($_SESSION[‘u_auth’], 2, “www.someotherplace.com/somepage.php”);  // redirects to www.someotherplace.com/somepage.php if the user isn’t auth level 2


. . . 

I hope you’ll find some of the content useful

References;

查看更多
后来的你喜欢了谁
4楼-- · 2018-12-30 22:32

1. Using header in-build php function

a) Simple redirect without parameters

<?php

   header('Location: index.php');

?>

b) Redirect with GET parameters

<?php

      $id = 2;

      header("Location: index.php?id=$id&msg=succesfully redirect");

  ?>

2. Redirect with javascript in php

a) Simple redirect without parameters

<?php 

     echo "<script>location.href='index.php';</script>";

 ?>

b) Redirect with GET parameters

<?php 

     $id = 2;

     echo "<script>location.href='index.php?id=$id&msg=succesfully redirect';</script>";

   ?>
查看更多
柔情千种
5楼-- · 2018-12-30 22:34

Output JavaScript from PHP using echo, which will do the job.

echo '<script type="text/javascript">
           window.location = "http://www.google.com/"
      </script>';

You can't really do it in PHP unless you buffer the page output and then later check for redirect condition. That might be too much of a hassle. Remember that headers are the first thing that is sent from the page. Most of the redirect is usually required later in the page. For that you have to buffer all the output of the page and check for redirect condition later. At that point you can either redirect page user header() or simply echo the buffered output.

For more about buffering (advantages)

What is output buffering?

查看更多
弹指情弦暗扣
6楼-- · 2018-12-30 22:36
function Redirect($url, $permanent = false)
{
    if (headers_sent() === false)
    {
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }

    exit();
}

Redirect('http://www.google.com/', false);

Don't forget to die()/exit()!

查看更多
泪湿衣
7楼-- · 2018-12-30 22:36
<?php 
header('Location: redirectpage.php');
header('Location: redirectpage.php');exit();
echo "<script>location.href='redirectpage.php';</script>";
?>

This is regular and normal PHP redirect but you can make a redirecting a page with few second wait below code:

<?php
header('refresh:5;url=redirectpage.php '); //Note: here 5 means 5 seconds wait for redirect.
?>
查看更多
登录 后发表回答