SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Detecting what page you are on
-
May 19, 2009, 07:57 #1
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!
-
May 19, 2009, 08:36 #2Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
May 19, 2009, 11:02 #3
- 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.
-
May 19, 2009, 11:17 #4
- 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;
}
-
May 19, 2009, 11:30 #5
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
-
May 19, 2009, 11:32 #6
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 19, 2009, 12:09 #7
- 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
-
May 19, 2009, 12:45 #8
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 19, 2009, 15:21 #9
-
May 19, 2009, 16:27 #10
- Join Date
- Apr 2008
- Location
- Temecula, CA
- Posts
- 278
- 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