Reputation: 17
I want to only load script when it needed and am currently using php.
<?php
$page = basename($_SERVER['REQUEST_URI']);
if ($page == 'shows.php' || $page == "/" || $page == '') {
?>
<script></script>
What is the best practice for excluding script? Would excluding script help load time? If anyone has other suggestions I would be glad to hear them.
Problem: I want to get show.php from the url
http://www.turbosaw.com/shows.php?action=viewShow&showId=30
My previous php url query (what i'm calling it till someone corrects me), doesn't work, and I assume it's because it has all that extra stuff behind php.
Any ideas?
Upvotes: 1
Views: 211
Reputation:
You can use parse_url()
function for that purpose.
Code:
<?php
$url = "http://www.turbosaw.com/shows.php?action=viewShow&showId=30";
var_dump(parse_url($url));
?>
Output:
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(16) "www.turbosaw.com"
["path"]=>
string(10) "/shows.php"
["query"]=>
string(25) "action=viewShow&showId=30"
}
Hope this helps.
Upvotes: 0
Reputation: 26523
Firstly, yes you can use
parse_url()
, so this answer is more aimed at what you do with that once you've actually gotten it, which is the easy part.
You could just check that "shows.php" is contained within the url string:
if (stristr($url, 'shows.php')) { ...
Note: stristr() is case insensitive, strstr() is case sensitive
However, this doesn't look like the best way of doing things. You could be using regex to do this as well, but really it looks like you're trying to figure out some sort of routing mechanism.
What you could do is something like Symfony2's routing mechanism, which is have a pre-built "map" of routes to check for. It looks like you aren't using a framework and won't be anytime soon, so here's a simple suggestion:
Have an include file that simply checks if the url is in an array of url's, and if it is, output the code to include the JavaScript file.
$routes = array(
'/',
'/shows.php'
);
You could loop around the array and check if any of these routes is in the url, and do as you wish (ie, include the JS file):
foreach ($routes as $route)
{
if (in_array($route, $yourURL))
{
// Include your JS file here
break;
}
}
Do that in one file. Include it at the top of your script in a sort of 'bootstrap', and then you won't have to duplicate this check on every single page you use. You just include the 'check file'.
Upvotes: 0
Reputation: 1130
You could use the parse_url function in PHP.
$url = "http://www.turbosaw.com/shows.php?action=viewShow&showId=30";
$parsed_url = parse_url($url);
$path = $parsed_url['path']; // '/shows.php'
//Now to take off front '/'
$path = substr($path, 1); // 'shows.php'
Upvotes: 1
Reputation: 21684
You can use PHP's built-in parse_url()
function.
$url = 'http://www.turbosaw.com/shows.php?action=viewShow&showId=30';
print_r(parse_url($url));
Would output:
Array
(
[scheme] => http
[host] => turbsaw.com
[path] => /path
[query] => action=viewShow&showId=30
)
You can get your own server and page's addresses if you look into the $_SERVER
array.
Upvotes: 0
Reputation: 4383
You can get the filename of the script using PHP magic constants:
ie echo __FILE__;
would give you shows.php for the above URL
Upvotes: 0