Redirection if a condition is met

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aeldaly@gmail.com

    #1

    Redirection if a condition is met

    I know this topic has been discussed a few times here and also on
    php.net, but I cannot find an answer to my specific problem. I check to
    see if a user is logged in before serving admin.php like so:
    <?php
    session_start() ;
    if (!isset($_SERVE R['username']) or !isset($_SERVER['password'])) {
    header("locatio n:http://linux-place.com");
    exit;
    }
    ?>
    These are the very first lines in the file. It works perfectly. The
    problem is that I would like to output a message like "Please login
    first before accessing the administration panel". How can I do that
    without getting the headers already sent error?

    Thanks,
    Ahmed El-Daly

  • chotiwallah

    #2
    Re: Redirection if a condition is met



    aeldaly@gmail.c om wrote:[color=blue]
    > I know this topic has been discussed a few times here and also on
    > php.net, but I cannot find an answer to my specific problem. I check to
    > see if a user is logged in before serving admin.php like so:
    > <?php
    > session_start() ;
    > if (!isset($_SERVE R['username']) or !isset($_SERVER['password'])) {
    > header("locatio n:http://linux-place.com");
    > exit;
    > }
    > ?>
    > These are the very first lines in the file. It works perfectly. The
    > problem is that I would like to output a message like "Please login
    > first before accessing the administration panel". How can I do that
    > without getting the headers already sent error?
    >
    > Thanks,
    > Ahmed El-Daly[/color]

    i generally use a 3 step procedure:

    1. check if headers are sent, if not use header to redirect

    2. if headers are sent already, print a javascript relocation routine

    3. as a fallback if javascript is disabled, print a normal link

    micha

    Comment

    • Jean-Baptiste Nizet

      #3
      Re: Redirection if a condition is met

      Is http://linux-place.com the URL of the login page?
      If it is, why not passing the message to this URL (or a message key):

      <?php
      session_start() ;
      if (!isset($_SERVE R['username']) or !isset($_SERVER['password'])) {

      header("locatio n:http://linux-place.com?messa geKey=loginBefo reAdminPanel");
      exit;
      }
      ?>

      and, in the login PHP page :

      <?php
      $message = getMessageFromK ey($_GET["messageKey "]);
      // display the message
      // display the login form
      ?>

      You should even pass the URL of the page requested before the redirect
      to the login page, and redirect to this URL once the login is
      successful. (Or put this URL in the session before the redirect to the
      login page, and redirect to this URL after the login is successful).

      JB.

      Comment

      • aeldaly@gmail.com

        #4
        Re: Redirection if a condition is met

        YES!!! Thanks JB. That is exactly what I am going to do.

        Comment

        Working...