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);
Related
I want to fetch full URL path "http://127.0.0.1:8000/admin/2#document" using PHP or Laravel framework. I have tried to fetch using $_SERVER['REQUEST_URI'] but it did not work.
If I got your question right, you want to get the current URL of a page you are on? If that's the case, there's a url() helper method which outputs the current URL you're on. Inside your controller, do this:
$url = Request::url();
echo $url;
EDIT:
If you have some queries present in your logic, you can use getRequestUri() method:
$url = Request::getRequestUri();
echo $url;
You can read more in official documentation.
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";
How can I read last part (after slash) of url if my url is like this:
http://testWeb.com/eng/pay-online/#__hc-action-complete--9d79883508
I've tried with $_GET and $_POST but these are empty. Also I've tried using $_SERVER['REQUEST_URI'] but it also return url till "pay-online". The project is in wordpress (in case there is something in WP which can solve this).
You can obtain the fragment using JS like:
var fragment = location.hash.substring(1); // minus the hash
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");
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);