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:26

you can update the header in php: header

查看更多
何处买醉
3楼-- · 2018-12-30 22:28

I've already answered this question, but I'll do it again since in the meanwhile I've learnt that there are special cases if you're running in CLI (redirects cannot happen and thus shouldn't exit()) or if your webserver is running PHP as a (F)CGI (it needs a previously set Status header to properly redirect).

function Redirect($url, $code = 302)
{
    if (strncmp('cli', PHP_SAPI, 3) !== 0)
    {
        if (headers_sent() !== true)
        {
            if (strlen(session_id()) > 0) // if using sessions
            {
                session_regenerate_id(true); // avoids session fixation attacks
                session_write_close(); // avoids having sessions lock other requests
            }

            if (strncmp('cgi', PHP_SAPI, 3) === 0)
            {
                header(sprintf('Status: %03u', $code), true, $code);
            }

            header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
        }

        exit();
    }
}

I've also handled the issue of supporting the different HTTP redirection codes (301, 302, 303 and 307), as it was addressed in the comments of my previous answer, here are the descriptions:

  • 301 - Moved Permanently
  • 302 - Found
  • 303 - See Other
  • 307 - Temporary Redirect (HTTP/1.1)
查看更多
人气声优
4楼-- · 2018-12-30 22:28

We can do in two way

  1. when user come on https://bskud.com/PINCODE/BIHAR/index.php then redirect to https://bskud.com/PINCODE/BIHAR.php

by below php code

<?php header("Location: https://bskud.com/PINCODE/BIHAR.php"); exit; ?>

Save Above code in https://bskud.com/PINCODE/BIHAR/index.php

2.When any condition true then redirect to other page

<?php  $myVar = "bskud";   if ($myVar == "bskud") { ?>  <script> window.location.href="https://bskud.com";  </script> <?php  } else {  echo "<b>Check Website Name Again</b>"; } ?>

`

查看更多
有味是清欢
5楼-- · 2018-12-30 22:28

If you're running on Apache you can also use .htaccess for redirect.

Redirect 301 / http://new-site.com/
查看更多
无色无味的生活
6楼-- · 2018-12-30 22:30

In the eve of the semantic web, correctness is something to consider. Unfortunately, PHP's "Location"-header still uses the HTTP 302-redirect code, which, strictly, isn't the best one for redirection. The one it should use instead, is the 303 one.

W3C is kind enough to mention that the 303-header is incompatible with "many pre-HTTP/1.1 user agents," which would amount to no browser in current use. So, the 302 is a relic, which shouldn't be used.

...or you could just ignore it, as everyone else...

查看更多
浅入江南
7楼-- · 2018-12-30 22:31
<?php header('Location: another-php-file.php'); exit(); ?>

or if you have already opened php tags, use this:

header('Location: another-php-file.php'); exit();

You can also redirect to external pages, eg:

header('Location: https://www.google.com'); exit();

Make sure you include exit() or include die()

查看更多
登录 后发表回答