简体   繁体   中英

WordPress header location redirect

having a problem here with WordPress. I want to redirect the page to a specific .php file inside a folder (php/adminpage.php) whenever $_SESSION variable is equals to 1. Let's say the session variable is 1:

<?php
 if ((isset($_SESSION['login']) && $_SESSION['login'] == '1')) {
 header ("Location: php/adminpage.php");
?>

But the browser returns "Not Found". Any ways to get it to work?

UPDATE [SOLVED]: Using the full path works. Thanks to @andrewsi. Working code:

<?php session_start();  
if ((isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: wp-content/themes/euro/php/adminpage.php");
}

?>

You're using a relative path:

 header ("Location: php/adminpage.php");

That will look for a folder below where the current file is, and look for adminpage.php in there.

But WordPress does some funny things with page names and .htaccess, so your current page might not be where you expect it to be; it's generally always better to use a full path, which is a lot less ambiguous:

header("Location: /wp-content/themes/euro/php/adminpage.php");

And don't forget to exit after calling it, too, so code execution stops on the page from which you are redirecting.

Is this an actual URL location?

header ("Location: php/adminpage.php");

To my eyes it seems like a file system path. Because is your local setup at this URL:

localhost

And then this would be the location of that file?

localhost/php/adminpage.php

Also, I would clean up your code like so:

<?php
if (array_key_exists('login', $_SESSION) && isset($_SESSION['login']) && intval($_SESSION['login']) == 1)) {
  header("Location: php/adminpage.php");
  }
?>

By using array_key_exists you prevent unset index errors & by using intval you are assured there is a numerical value in place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM