Search code examples
phphttp-redirectuserid

php session id redirect


What I am try to do is use $_SESSION['user_id'] to check if 'user_id' is = to "number", say 56 for example, if so load page, if not redirect user to "billing/".$_SESSION['user_id'].".php";

So far I have this

<?php
    if ($_SESSION['user_id']) === 56) {
        //do nothing
    } else {
          header("Location: billing/".$_SESSION['user_id'].".php");
          exit();   
    }
?>

I know this code is wrong but hopefully it conveys what I am trying to accomplish. Thanks in advance for your help and code snippets.


Solution

  • You're pretty close, but you could use the php function http_redirect instead for the redirect, to be more concise (requires the PECL library):

    <?php
        if ($_SESSION['user_id'] == 56) {
            //do nothing
        } else {
            http_redirect("billing/".$_SESSION['user_id'].".php", array(), true, HTTP_REDIRECT_PERM);
        }
    ?>