Home > OS >  Trying to redirect to another page
Trying to redirect to another page

Time:09-16

Long story short, I am having an issue getting from one page to another. When I create a user, I am suppose to go to the login page. Instead I get https://localhost/TEST/ROOTlogin . Not entirely sure what I am doing wrong but may someone please lead me the right way?

if($result != "") {

        echo "<div style='text-align:center;font-size:12px;color:white;background-color:grey;'>";
        echo "<br>The following errors occured:<br><br>";
        echo $result;
        echo "</div>";

    } else {

        header("Location:". ROOT . "login");
        echo header;
        die;

    }

<?php

ini_set("display_errors", 1);

function split_url() {

    $url = isset($_GET['url']) ? $_GET['url'] : "home";
    $url = explode("/", filter_var(trim($url,"/"), FILTER_SANITIZE_URL));

    return $url;

}

$root = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$root = trim(str_replace("index.php", "", $root), "/");

define("ROOT", $root . "/");

$URL = split_url();

if(file_exists($URL[0] . ".php")) {

    require($URL[0] . ".php");

} else {

    require("404.php");

}

CodePudding user response:

You need to determine if your redirect is really what you think it is. So the first step is to print out the redirect string instead of redirecting.

$header = 'Location: ' . ROOT . ‘login';
die($header);

// header($location);

From here, you will know which way to go with your debugging.

If $header is not what you expect, use the same technique to debug where the mistake is being made.

If it is what you expect, but it’s redirecting somewhere else, you know it’s your server’s rewrite. (Hint: type in the redirect url directly into the browser to test)

TBH, I would just drop ROOT and use header(‘Location: /login');, assuming that your rewrite is doing the right thing.

CodePudding user response:

You can try it this way:

<?php

ini_set("display_errors", 1);

function split_url($url = 'home') {
    $url = explode("/", filter_var(trim($url,"/"), FILTER_SANITIZE_URL));
    return $url;
}

$url = empty($_GET['url']) ? '' : $_GET['url'];
$URL = split_url($url);

$root = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$root = trim(str_replace("index.php", "", $root), "/");
define('ROOT', $root . '/');

if(file_exists($URL[0] . ".php")) {
    require($URL[0] . ".php");
} else {
    require("404.php");
}

  • Related