ok i have a url from $_SERVER['REQUEST_URI']
lets say it give us a url
http://localhost/controller/method
i have tried something like
explode('/',$_SERVER['REQUEST_URI'])
and it gave us like
array
0 => string '' (length=0)
1 => string 'controller' (length=10)
2 => string 'method' (length=6)
what is the best way to get the controller or method ? or removeing the 0 in the array ? ( first array ) ?
so it will be like
$controller = 'controller';
$method = 'method';
from above inputs.
maybe about list ? still no clue using list().
edit heres what ive done so far
$this->url = str_replace(config('foldertoscript'), NULL, $_SERVER['REQUEST_URI']);
$hello = explode('/',$this->url);var_dump($hello);
array_shift($hello);
list($controller,$method) = $hello;
var_dump($hello,$controller);
in a class
Thanks for looking in.
Adam Ramadhan
To remove the first element of an array, you can use array_shift().
$_SERVER['REQUEST_URI'] gives you the url without the "http://www.yoursite.com".
You can use something like this
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
echo curPageURL();
?>
Hope this helps.
Use array_shift to remove the first array item.
http://php.net/manual/en/function.array-shift.php
Example:
$your_array = array_shift($your_array);
$controller = $your_array[0];
$method = $your_array[1];
For the same matter I use url_rewriting.
I have a rule that says ^([a-zA-Z0-0-_\/.]+)$ index.php?url=$1
(this is not a copy paste from my code, but you get the idea)
then if you say $_URL = $_REQUEST["url"];
$directive = explode("/",$_URL);
you will get what you need, as for the parameters you could say module/method/id/1/data/2
you have to take care of your parameters and it works if you use the GET method for
navigation only(as it should be used). Also makes the stuff much safer as no one can send SQL
injections via get or any "smart" directives.
Related
I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php
I have a small script which should get the meta title of the current page the script is added into. The problem is, that its working fine on several test pages, but not into my CMS. It loops until death there and I cant reach any page on my server until I restart apache completely and by taking the script off.
May someone take a look at it? This would be really awesome since I used google for hours and sure, I found X threads and pages, but never a solution for this special loop-effect.
<?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
preg_match ("/<title>([^`]*?)<\/title>/", $data, $match);
$urltitle = $match[1];
}
?>
<?echo $urltitle;?>
The $_SERVER["REQUEST_URI"] can also include GET params like this:
mysite.com?param1=1¶m2=2
Then you try to append a string ?ignore=this so you get
mysite.com?param1=1¶m2=2?ignore=this
which is translated by PHP into variables like
param1 = '1'
param2 = '2?ignore=this'
You must check for ? symbol in the $url variable
I'm using this function to get the current page url :
function currentURL() {
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['SERVER_NAME'];
$port = $_SERVER["SERVER_PORT"];
$query = $_SERVER['REQUEST_URI'];
return $protocol.'://'.$host.($port != 80 ? ':'.$port : '').$query;
}
But your problem comes from here :
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
/* ... */
}
This will work with "test pages", but you CMS propably use url-rewriting, which can cause the lost of your $_GET['ignore'] variable : if you've already other GET variable for example.
You should have a look into your .htaccess files, or read your CMS documentation to know what can change you url.
Anyway, it seems you're building some unstable code, and this only to get the page title. I'm pretty sure you've got another way to get it easily with your CMS.
I found this code on the internetz, it checks the current page url;
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
So now I can do something like this;
elseif (curPageURL() == "http://www.example.com/pageexample") {
<meta tags here>
}
Great. But I would also like to use this for pagination pages. Those URLs look like this:
http://www.example.com/pageexample?start=30&groep=0
http://www.example.com/pageexample?start=60&groep=0
http://www.example.com/pageexample?start=90&groep=0
[....]
http://www.example.com/pageexample?start=270&groep=0
I could use a if statement for every of those links.. but I would much rather like to use one. Is it possible to add a wildcard or something? Like this I guess (notice the *)
elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {
edit: I would like to do this for all those URLs because I want to give them the same meta description, <title> and <link rel="canonical". I could do this manually by doing an if-statement for every page (10+ atm) but I figured there was a better way.
Why not just use the parse_url() function? From the manual page:
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
?>
// The above would print
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
For your particular case, you could then just check against the host and path variables.
Sounds a lot like regex problem:
if (preg_match("#^http://www.example.com/pageexample(\?start=[^&]*&groep=0)?#", curPageURL())) {
// it matches
}
The expression [^&]* acts like your *.; to match non-empty items, use[^&]+`. It matches these:
http://www.example.com/pageexample
http://www.example.com/pageexample?start=30&groep=0
Update
It's not entirely clear why you need to compare against the full canonical URL, unless you have multiple domains point to the same code base.
You should use a string comparison function
if (strstr(curPageURL(), 'http://www.example.com/')) !== FALSE) {
// curPageURL() contains http://www.example.com/
}
or
if (preg_match('/^http\:\/\/www\.example\.com\//', curPageURL()) {
// curPageURL() starts with http://www.example.com/
}
There's lots of ways to do it
You could wrap this
elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {
in a while loop that adds 30 to a variable where you have your wild card on each iteration.
did you try regex?
if (preg_match('/http:\/\/www\.example\.com\/pageexample\?start=[0-9]+&groep\=0/i', "http://www.example.com/pageexample?start=34&groep=0")) {
echo "A match was found.";
else {
echo "A match was not found.";
}
If you don't use the query_string element from the $_SERVER array all your paginated URLs will return the same URL: http://www.example.com/pageexample, you can check with
echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ;
vs
echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'?'.$_SERVER["QUERY_STRING"] ;
You'll see that in the first case you don't receive GET parameters
If I have url like this: www.example.com/product/yellow-bed is it possible to retrieve product name from url?
For example, if url would be like www.example.com?page=product&product_name=yellow_bed I would use:
$product = $_GET['page'];
$product_name = $_GET['product_name'];
But how to get it from www.example.com/product/yellow-bed ?
Get the URI by "$_SERVER["REQUEST_URI"]". Convert the string into an array with explode:
$uri = 'www.google.com/product/yellow-bed' //uri = $_SERVER["REQUEST_URI"]
$uriArray = explode('/', $uri);
$product = $urlArray[1];
$product_name = $urlArray[2];
0 = www.google.com, 1 = product, 2 = yellow-bed
PHP manual: array explode ( string $delimiter , string $string [, int $limit ] ).
Some php frameworks (like CodeIgniter) has already this function implemented.
Never the less you can have a look here: http://erunways.com/simple-php-get-uri-or-segment-element/ , and that should solve your problem.
You could fetch the complete URI with $_SERVER['QUERY_STRING'] and then split it apart. Or you work with an .htaccess and internally rewrite the url.
Try $_SERVER[ 'PATH_INFO' ] or $_SERVER[ 'REQUEST_URI' ].
You should have a look at $_SERVER['REQUEST_URI']. This will be /product/yellow-bed in your example.
You can then use strrpos(), explode() orpreg_match()` (or other string manipulation functions) to extract what you want.
you can use the following function to retrive current url:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
now you can take url and parse its elements using parse_url : http://php.net/manual/en/function.parse-url.php
I have the following function that get's the current page URL:
<?php
// get current page url
function currentPageUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo $pageURL;
}
?>
Which prints:
http://localhost/gallery.php?id=23&type=main
I want to remove "&type=main" which is present in the url. So before echoing $pageURL I add the following line:
$pageUrl = preg_replace("&type=main", "", $pageURL);
But it still returns the full url including type=main. How can I get rid of that from the url?
Another solution could be to :
use parse_url or $_SERVER['QUERY_STRING'] to extract the list of parameters as a string
use parse_str to transform the query string to an array containing each parameter and its value -- indexed by parameters names.
Do some magic on that array :
do what you have to to filter it
For example, unset($array['type']); could probably help ;-)
If needed, add more parameters to that array
And, then, use http_build_query to re-build a query-string.
A bit more complex than string manipulations, of course -- but much more reliable, I'd say ;-)
You can throw a url into parse_url. It will return an array from which you can rebuild as you see fit.
Try this:
$pageUrl = str_replace('&type=main', '', $pageURL);
did you try any other $_SERVER variables?
there are plenty and some of them already contain everything you need without any replace
phpinfo(32);
will show you all
PHP identifiers are case sensitive. You probably meant to assign it to the same variable.
$pageURL = preg_replace("&type=main", "", $pageURL);
Either that, or you need to change the remnant of code to use $pageUrl instead of $pageURL.