Check Page Request

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    Check Page Request

    Basically I have a redirection for all pages to send the user to the login page if they are not logged in. The problem is, I am using the same headers, footers etc for every page, so I wrote this to fit in somewhere:

    [PHP]if ( $_SESSION['user_name'] == NULL ) {
    echo( " <meta http-equiv=\"refresh \" content=\"0;url =login.php\"> " );
    }[/PHP]

    It works but it still loads the whole (or atleast most of the) page before it redirects, which I can't have.

    So I want to only continue loading the page if it passes, and in all other reasons, redirect to my login page: using something like:

    [PHP]if ( $_SESSION['user_name'] == NULL && $_REQUEST != login.php ) {
    echo( " <meta http-equiv=\"refresh \" content=\"0;url =login.php\"> " );
    }[/PHP]

    Just so it doesn't redirect on the login page into a loop. What is the command that should be in place of "$_REQUEST != login.php"?

    TS
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by TheServant
    Basically I have a redirection for all pages to send the user to the login page if they are not logged in. The problem is, I am using the same headers, footers etc for every page, so I wrote this to fit in somewhere:

    [PHP]if ( $_SESSION['user_name'] == NULL ) {
    echo( " <meta http-equiv=\"refresh \" content=\"0;url =login.php\"> " );
    }[/PHP]

    It works but it still loads the whole (or atleast most of the) page before it redirects, which I can't have.

    So I want to only continue loading the page if it passes, and in all other reasons, redirect to my login page: using something like:

    [PHP]if ( $_SESSION['user_name'] == NULL && $_REQUEST != login.php ) {
    echo( " <meta http-equiv=\"refresh \" content=\"0;url =login.php\"> " );
    }[/PHP]

    Just so it doesn't redirect on the login page into a loop. What is the command that should be in place of "$_REQUEST != login.php"?

    TS
    Use a header redirect at the very start of your script:
    [php]
    <?php
    if(@$_SESSION['user_name'] == NULL)
    {
    header("Locatio n: login.php");
    exit; # quit the script.
    }
    [/php]
    Regards :)

    Comment

    • nitinpatel1117
      New Member
      • Jun 2007
      • 111

      #3
      you can keep the echo, just put the exit after it on the next line.


      [PHP] if ( $_SESSION['user_name'] == NULL ) {
      echo( " <meta http-equiv=\"refresh \" content=\"0;url =login.php\"> " );
      exit;
      }[/PHP]

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Thanks guys,
        But I still would like to know if there's a way to check which page is being requested? I have done what both of you suggested before, but it is not really as neat as I want (I'm pedantic on neatness), so it could be better if I could get that if statement to include a page name checking like I said before.

        Thanks for your help.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by TheServant
          Thanks guys,
          But I still would like to know if there's a way to check which page is being requested? I have done what both of you suggested before, but it is not really as neat as I want (I'm pedantic on neatness), so it could be better if I could get that if statement to include a page name checking like I said before.

          Thanks for your help.
          Sure there are ways to check which page.. but i'm not sure you need to check.
          If the script is in a page called test.php you'd never have to check if the page was login.php because you know the page is test.php

          Could you explain why you have to check?

          You could get the filename using
          [php]
          $_file = $_SERVER['SCRIPT_NAME']; # get script location
          $_file = explode("/", $_file); # explode into array
          $_fileName = $_file[count($_file) - 1]; # last
          echo $_fileName;
          [/php]
          Regards

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Thanks for that.
            Well, I guess it is being very silly, but I have header_a.php, header_b.php and footer.php. So hwo my page is structured is:
            [php]<?php include('header _a');
            echo("<title>Pa ge Title</title>");
            include('header _b');
            {Page Content}
            include('footer ');
            ?>[/php]

            So header_a has session_start() , and everything upto and including <head>, header_b has any extra required files (CSS etc) and </head> and the <body> tag. Then the content, and then footer contains end of page files </body></html>

            This way I only have one file to edit the look of all my pages, and that's why I wanted to include that bit of code you gave in the header_a file. But I might do it the previously mentioned ways by altering how my header_a works. Thanks for your help guys.

            Comment

            Working...