'PHP_SELF' and 'SCRIPT_NAME' Questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgourd
    New Member
    • Apr 2010
    • 25

    'PHP_SELF' and 'SCRIPT_NAME' Questions

    Hello,

    I am currently trying to write a quick form that I want to appear on all my pages. What I basically do is create 3 files for each page.
    1. header.php
    2. file.php
    3. footer.php

    The header and footer files stay the same site-wide and I just include them in 'file.php' in order to avoid retyping hundreds of lines of code. I am thinking about putting a form like this in the 'footer.php' file:
    Code:
    <form name="login" action="file.php" method="post">
    The only problem I have is that the name of 'file.php' changes depending on the page I have.

    I was wondering if I used $_SERVER['PHP_SELF'] or $_SERVER['SCRIPT_NAME'] in 'footer.php' would it return the path to 'footer.php' or to 'file.php'.

    I have also seen $_SERVER['REQUEST_URI'] around and that might be another option.
  • chathura86
    New Member
    • May 2007
    • 227

    #2


    Regards

    Comment

    • dgourd
      New Member
      • Apr 2010
      • 25

      #3
      Originally posted by chathura86
      I have read the manual for those variables, but I want to know if it gives the path of the file in the url or the included 'footer.php' file.

      For example, the url will be www.example.com/file.php, but the global variable will be run from 'footer.php' which is included in 'file.php' I want to know which one will show 'file.php' instead of 'footer.php'. I am leaning toward 'REQUEST_URI' but I am not sure.

      Comment

      • chathura86
        New Member
        • May 2007
        • 227

        #4
        all of them will give the file.php

        example

        inc1.php
        Code:
        <?php
        	echo $_SERVER['PHP_SELF'] . '<br>';
        	echo $_SERVER['SCRIPT_NAME'] . '<br>';
        	echo $_SERVER['REQUEST_URI'] . '<br>';
        	echo '<br>---------------------------------------' . '<br>';
        ?>
        page1.php
        Code:
        <?php
        	echo $_SERVER['PHP_SELF'] . '<br>';
        	echo $_SERVER['SCRIPT_NAME'] . '<br>';
        	echo $_SERVER['REQUEST_URI'] . '<br>';
        	echo '<br>---------------------------------------' . '<br>';
        
        	include ('inc1.php');
        ?>
        my request is http://localhost/test/page1.php?a=1

        Code:
        /test/page1.php
        /test/page1.php
        /test/page1.php?a=1
        
        ---------------------------------------
        /test/page1.php
        /test/page1.php
        /test/page1.php?a=1
        
        ---------------------------------------
        as you an see that REQUEST_URI has url parameters also

        i useally use PHP_SELF but if i need the url parameters also
        then i use REQUEST_URI

        Regards

        Comment

        • dgourd
          New Member
          • Apr 2010
          • 25

          #5
          Thank you very much for the clarification. Finally, with REQUEST_URI, does that also include information sent through the POST method?

          Comment

          • chathura86
            New Member
            • May 2007
            • 227

            #6
            nope post date will not be included

            Regards

            Comment

            • dgourd
              New Member
              • Apr 2010
              • 25

              #7
              Thanks alot for all of your help.

              Comment

              Working...