phpfunctionsuperglobals

How to redirect to a page according to URL parameter in php?


I am working on project in php where I want to create parameter in main index page and redirect page according to the parameter Example: if URL is "index.php?page=about.php" the page shoudld be redirected to about.php

My code

function get_parameter(){
$page = $_GET['page'];
header("Location : ".$page".php");
}

I want to try it here

<?php if(!isset($_SESSION['userId'])): ?>
    <li class="nav-item">
        <a class="nav-link" href="index.php?page=login">Sign In</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="index.php?page=register">Sign Up</a>
    </li>
<?php endif; ?>

So how do i make the function working? thankyou


Solution

  • If the URL is "index.php?page=about.php" and $page = $_GET['page']; then $page = about.php.

    So the code for redirect should be header("Location: $page");

    Inside "" yo do not have to concatenate with point (.) the php $variables. You have to do that only with '' like: header('Location: ' . $page . '');

    And do not use a function for redirect. Just read the page from GET and then redirect.

    header() will not work if you load html.