-2

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
webmaster alex l
  • 663
  • 3
  • 16
  • 32

4 Answers4

1
  1. do not edit the code in your question based on the answers. You are making it impossible to understand what are you talking about.
    If you want to add something - ADD it below the original text.

  2. Ask clear, certain question. Describe the problem you face and what kind of solution you need.

  3. Separate matters. As a matter of fact, sessions has nothing to do with redirects. If you want to know how to use sessions - ask how to use sessions. If you already have valid and verified session variable but have no idea of redirects - ask about redirects. If you don't know how to compare values - ask it. If you know everything but not certain about some bells and whistles of the code styling - ask this particular question.

Now, what is your question?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

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);
    }
?>
Jez
  • 27,951
  • 32
  • 136
  • 233
0

Why not check the opposite? And for readability it is wise to use {} not "." (as long as your editor highlights this)

<?php 
if($_SESSION["user_id"] != 56) {
    header("location: billing/{$_SESSION['user_id']}.php");
    die();
}
?>
  • Whoever downvoted, care to explain? I was going to suggest this. It makes no sense to test for true, then only run code for false. This is the better way of doing it for readability. – Anonymous Oct 03 '11 at 15:45
  • I didnt downvote but I couldnt get the redirect to work? No matter, the code still kills the page from loading if the user_id is not correct. Thanks for your help :) – webmaster alex l Oct 03 '11 at 16:14
0

The PHP Doc for header() gives a lot of info about how to use the header function. The most important is:

  • Make sure no html or text has been echoed or sent to the browser before you call header().
  • You need an exit(); after header() since you're done rendering the page and some browsers prefer it.
  • HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path so you'll want to put the full URL after 'Location:'. See php doc for example.
Weboide
  • 1,112
  • 1
  • 11
  • 22