How to get full url along with # symbol in php - php

I am using AngularJS routing with php,
so that my urls look like
http://localhost/admin/home#/categories
When ever I am trying to get url it wont gave full url. It gives only /admin/home.
I am using $_SERVER['REQUEST_URI'] and codeigniter segment. Both are not working.
Can any one help?

This information is not being past to the server I'm afraid.
The url part after # is only for browser use. You can add GET or POST parameters to pass this information to your server like:
http://localhost/admin/home?hash=categories#/categories

use:
$_SERVER[PHP_SELF]
It will give the full url, including everything.
If you want to strip stuff from the url, use things like this:
$url = trim(strtok("$url", '?'));
$url = str_replace("#!/", "", "$url");

Related

Get custom URL PHP

I'm working on a simple app to retrieve a JSON file info, but to do so I need an ID, I fixed that problem by adding it to the URL like this:
http://localhost:8000/comic_library/comic.php?id=2097
Since this is not a friendly URL, I change so it looks like this:
http://localhost:8000/comic_library/comic/2097
But when I retrieve the URL, the only thing I got back is:
http://localhost:8000/comic_library/comic.php
But I need the id in order to retrieve the JSON file, is there a way of getting the custom URL and not just the absolute URL
As you are using core php, it is not possible for you to have an url like http://localhost:8000/comic_library/comic/2097, if you used a framework like codeigniter or cms like wordpress then you would get that url. Core php urls have .php extension.
You should Pass the URL like this http://localhost:8000/comic_library/comic.php/2097
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
echo $uri_segments[2]; // for http://localhost:8000/comic_library/comic.php/2097 you will get '2097'
First split the URL segments and Get the desired segment array value.
if your url is this
http://localhost:8000/comic_library/comic/2097
you can get your id by the following process
$fullurl = 'http://localhost:8000/comic_library/comic/2097';
$exploded_url = explode('/',$fullurl);
echo $final_id = end($exploded_url);

How to use $_GET when there is &038; at place of & in url

I have wordpress template in which URL is generating in for of xyz.com/?pickup_location=662#038;pickup_date=2018%2F08%2F08&return_date=2018%2F08%2F10
you can see there & at a place of & so when I trying to get value by $_GET['pickup_date'] it showing blank.
I used $_SERVER['REQUEST_URI'] to get complete URL but it's also not giving me complete URL
it's look like there is use of esc_url() wordpress function.
if anyone can help me to get this complete url as a string it can also work for me I could split a string with a parameter name
Your problem is the #.
Everything that comes after the # is only accessible by the client and not the server.
If you need a # in your URL like that you need to URL encode it:
urlencode('#'); // %23
In your case that will be something like this:
$url = "xyz.com/?pickup_location=" . urlencode("662#038;") . "pickup_date=2018%2F08%2F08&return_date=2018%2F08%2F10";

Pass a full url with GET parameters via GET

I'm trying to create a intermediary page, i.e user clicks on a link that leaves the site a page tells you that you're now leaving the site.
An example link would look like this:
http://example.com/transitionpage.php?r=http://www.google.com
transitionpage.php then works with a simple
$redirectto = $_GET['r'];
header( "refresh:2;url=".$redirectto );
However I'm running into the problem that if the url you're redirecting to also has multiple GET parameters in it, the domain gets cut off at the first occurrence of &
So if the link was originally:
http://example.com/transitionpage.php?r=http://www.google.com?par=1&par=2
It would become:
http://example.com/transitionpage.php?r=http://www.google.com?par=1
Which is unfavorable.
How do I pass on the full URL via GET without it getting chopped off ? Do I have to escape it ?
You can do URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp
Your get query would translate to: http://example.com/transitionpage.php?r=http%3A%2F%2Fwww.google.com%3Fpar%3D1%26par%3D2
These are the PHP methods you need: http://php.net/manual/en/function.urlencode.php http://php.net/manual/en/function.urldecode.php
Use:
$link = 'http://example.com/transitionpage.php?r='. urlencode('http://www.google.com?par=1&par=2');
Use the built in function for Encoding Urls
for example
urlencode("http://www.google.com");
for function refrence
Urlencode Php

PHP - Get path minus root

So I have this link:
http://kenthomes.net/Amelia-Cove (We use an alias system.)
Then I open a pop-up iframe (http://kenthomes.net/shareplan.php?mod=39)
How I can I pass the string "Amelia-Cove" to that page?
My best guess was to make the link = to http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
But how to I retrieve only "Amelia-Cove" from the initial page?
Try this:
$_SERVER['REQUEST_URI']
In your above example, this would have a value of: /Amelia-Cove. If you want to get rid of the /, try this:
trim($_SERVER['REQUEST_URI'],'/');
You can use the GET protocol to receive parameters in the URL:
http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
then fetch these parameters through the PHP GET global variable:
$_GET['plan']
I'm assuming OP will not always fetch Amelia-Cove but rather want that part of the url and used Amelia-Cove as an example and knows how to use $_GET
To fetch that part of the url and pass it through $_GET I would suggest using this:
parse_url($url, PHP_URL_PATH)
Full parse_url documentation

How to detect if user go to a link look like our rule?

example:
I have a webpage abc.com/index.php
I want to detect if user connect to abc.com/index.php/bbdh,
abc.com/index.php/bb/saggfd/gjasgd/hsahgd, abc.com/index.php/bb/saggfd/gjas and so on without having a page like that on my host.
I'm using php and need some way to do that without using .htaccess file.
Thanks for any help!
have a look at $_SERVER['REQUEST_URI'], every thing you need will be accessible here. E.g. for:
abc.com/index.php/bb/saggfd/gjas
it would be:
/index.php/bb/saggfd/gjas
strip the /index.php with something like:
echo substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
and you're set.
Update on how to handle GET-parameters:
// get the full request uri
$requestUri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
// let php parse that url (though it's just a part of the full url)
$chunks = parse_url($requestUri);
// let php parse the query string
parse_str(isset($chunks['query']) ? $chunks['query'] : '', $chunks['query']);
// debug output
print_r($chunks);

Categories