I have a link. Eg. abc.com/qwerty. I want to extract the part after / of every input just like examples below and use it just like a PHP GET input value and store it to a variable $page. Essentially, the link abc.com/qwerty should work like abc.com/proc.php?x=qwerty
Typed link Part to be used as PHP GET input
abc.com/cvbx cvbx
abc.com/ghvs ghvx
abc.com/pabc pabc
How can I do this?
You can use: $_SERVER['REQUEST_URI']
$request_uri = $_SERVER['REQUEST_URI']; //returns '/cvbx'
$segments = array_filter(explode('/', $request_uri)); //array_filter to remove empty elements.
You can parse the url as described at http://php.net/manual/en/function.parse-url.php .
If you have urls as described there, you can get the path part with:
$input = substr(parse_url($url, PHP_URL_PATH),1);
substr is to remove the starting /
Related
I need to create a variable in PHP from a URL, which does not have a fully formed query string.
e.g. http://search.domain.com/domain2.com
In this example, the variable needs to be
$website='domain2.com'
Is there a way to convert the entered URL in address bar to my ?website= variable?
An example would be the whois.domaintools service, which allows you to query a whois record from their website using the following url format:
http://whois.domaintools.com/domain.com
This then displays info based on the url you specified.
Can i achieve this using a MOD_Rewrite in the .htaccess, or can i use some PHP function like http_build_query to achieve this? I'm going around in circles and surely missing something obvious!
You can use this code to get your array $urlpart
$link = $_SERVER['REQUEST_URI'];
$urlpart = explode('/',trim(parse_url($link, PHP_URL_PATH), '/'));
Right now I'm printing the current URL I have and it kinda does it ok:
$currentURL = basename($_SERVER['PHP_SELF']);
The problem is that it only prints
solutions.php
instead of printing
solutions.php?id=581298a7-6c08-11e3-9bea-742f689f29f1
Anybody knows why this is happening? I need to get to print the part of
?id=581298a7-6c08-11e3-9bea-742f689f29f1
Because then I will be performing a substring to get just the ID.
Use parse_url, which parses a URL and returns its various components in a usable array. You'll also want to make sure you're parsing the correct string; in this case use:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Full usage:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
print_r(parse_url($url));
This is expected behavior, because everything from the ? afterwards are not part of the filename. The parameters that you're looking for are in the $_GET superglobal.
I need to pass a url using a GET address. To give an example which was I have tried:
http://www.example.com/area/#http://www.example.com/area2/
I've also tried replacing the forward slashes with other characters but that doesn't seem to work. How would you pass a url in a GET?
As I have understood, you should use url_encode() and url_decode().
The function url_encode() lets you create a string that can be used as a link.
You should use it this way:
$link = 'goto.php?link=' . url_encode($_POST['target_site']);
And when you were going to redirect to the user defined site (eg), you can decode the parameter given this way:
$decoded_link = url_decode($_GET['link']);
// Now it's safe to use the given URL (for example I can redirect to there)
header('location: ' . $decoded_link);
Hope it helps.
The # character links to an anchor on the page. The browser will automatically scroll to the element with the id after the point sign. So that's not what you're looking for.
To pass a GET parameter, the syntax would be like this:
http://example.com/area?http://example.com/area2
Then, if you var_dump($_GET), you'll see your URL. But, if you have other fields you also want to pass in your URL, you can use key=value pairs, like so:
http://example.com/area?url=http://example.com/area2¶m1=a¶m2=b
In this case, your URL will be available in $_GET['url'].
For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule
I'm using WAMP and the root folder of my page is: http://localhost/projects/bp/
In a Worpress application and I want to give unique id's to the body tag of every Page. So I did the following:
<?php
$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
$page = str_replace("?s=","",$page);
$page = $page ? $page : 'default'
?>
<body id="<?php echo $page ?>">
When I clicked the Aboutpage the URL change to the following: http://localhost/projects/bp/about and $page showed the following value:projectsbpabout
What can I do so that $page just show the last word of the URL. In this case, about, I don't want the projectsbp part)?
Do I have to change something in the Wordpress routing?
I would use PHP's built-in path parsing functions to do this.
Use parse_url() to cut off the query and get only the path part:
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
(parse_url is used to parse full URLs but should work fine for this. The second parameter is available since PHP 5.1.2.)
Then use basename() to extract the file name:
$pagename = basename($path, ".php"); // Will remove `.php` suffix
Additional thougths
Depending on how your site is structured, this method will not necessarily make for unique IDs. What if about is a sub-page in /bp and /bc? (If that is possible in Wordpress.) You would have two different pages with the same ID. In that case, you may want to use the full path as an identifier, converting slashes into underlines:
<body id="projects_bp_about">
also from own experience, I recommend using classes for this to avoid ID collisions if a page is named like an already existing elements on the page!
<body class="projects_bp_about">
As / is your separator, first create an array of all the loose parts:
$parts = explode('/', $_SERVER['REQUEST_URI']);
Now you just want the last element:
$last_part = end($parts);
of course this can also be done in one go:
$last_part = end(explode('/', $_SERVER['REQUEST_URI']));
to get only the last bit you can use
$page = array_pop(explode('/', $_SERVER['REQUEST_URI']));
explode the string by '/' and get the last piece.
instead of using str_replace you can use..
$pageArr = explode("/",$page);
it will give you array with three values you can capture the last one as about