SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting what page you are on

    Hi there,

    I was wondering if anyone had any insight as to how to detect the current PHP script / web page that is being rendered. Currently I'm doing so by detecting the script file name (for example page1.php), however I know this isn't a good practice. I was wondering if their is any other way to do this without relying on the current pages file name.

    Thanks!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by scoobasteve1982 View Post
    Currently I'm doing so by detecting the script file name (for example page1.php), however I know this isn't a good practice.
    Why?
    If you want to know what script is being rendered, why wouldn't it be good practice to get the current scripts name?

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a sidenote, when you are using an MVC design pattern, it's very easy to get the current action from the request object.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Function I wrote to provide this functionality.

    PHP Code:
    public function getfile()
    {
        
    $file $_SERVER["SCRIPT_NAME"];
        
    $break explode('/'$file);
        
    $file $break[count($break) -1];
        return 
    $file;


  5. #5
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Why?
    If you want to know what script is being rendered, why wouldn't it be good practice to get the current scripts name?
    Because what if I want to mod rewrite the URL for SEO - basically search engine friendly URLS. Then this method of accessing the current pages filename will not work (at least I don't think so). So basically a page like this...

    my_script.php?id=5&sec=4 would be

    myscript/5/4

  6. #6
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scoobasteve1982 View Post
    Because what if I want to mod rewrite the URL for SEO - basically search engine friendly URLS. Then this method of accessing the current pages filename will not work (at least I don't think so). So basically a page like this...

    my_script.php?id=5&sec=4 would be

    myscript/5/4
    See above function. That should return just the script name "foobar.php" regardless of the format.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe that that's what he doesn't want, because it will expose the actual script's filename (if it works). (And you can just use basename(), by the way.)

    There's no dynamic way to accurately figure out what part of the URL to cut off if you don't have a consistent way that you create those URLs with. If you do have a consistent way, then only you would know what it was, so you would have to write the function. You can get the actual URL from REQUEST_URI and/or REDIRECT_URL (Apache). I don't remember which one is which. I always check a phpinfo() file I have set up for that. =P

  8. #8
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    (And you can just use basename(), by the way.)
    You learn something new every day! Thanks.

  9. #9
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    I believe that that's what he doesn't want, because it will expose the actual script's filename (if it works). (And you can just use basename(), by the way.)

    There's no dynamic way to accurately figure out what part of the URL to cut off if you don't have a consistent way that you create those URLs with. If you do have a consistent way, then only you would know what it was, so you would have to write the function. You can get the actual URL from REQUEST_URI and/or REDIRECT_URL (Apache). I don't remember which one is which. I always check a phpinfo() file I have set up for that. =P
    This is exactly what I meant. Unfortunately I'll have to decide on the way that I want to rewrite my urls...

  10. #10
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you aren't using a framework (that would do this kind of stuff for you), then you will probably need to create a function that is loaded in your site config, and allows you to output the modified url. Since you use regex in the form of a rewriterule, then simply use a similar regex inside the function, applied to $_SERVER['PHP_SELF'], to return the value you are looking for.

    --added:

    This is just one reason why I've started using frameworks. I used to do all this stuff on my own, and now I can use a framework and get to work instead of worrying about the basics. Check out CodeIgniter at codeigniter.com. If you look through their user manual, you will be impressed with the stuff you don't have to do anymore.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •