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?

Read More

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");
}

?>

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.