How to get last part from a URL string in php? - php

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

Related

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";

unable to get value of get parameter in url with #

I am unable to get the value of a GET parameter in my URL.
My url is like
abc.com/new.php#tabs-11?val=100
I want to get value of val
<?php echo $_GET['val'];
This is not working, please tell what to do.
That URL makes no sense. You need to have the query string before the fragment:
abc.com/new.php?val=100#tabs-11
Stuff after # is not parsed as part of the URL by the server. It may not even be sent by the browser.
#tabs-11?val=100 would be considered an Anchor for the in this instance. Make sure that you put your Anchors at the end of the query string
When a url has a "#" in it, that points to a "named anchor", or a "fragment". It isn't part of the "GET" variables... So, you can't get it that way. In fact, the server doesn't even know anything about it.
Since you tagged this with jquery as well, javascript can actually do that.
var url = window.location.href;
This will give you the full url. From there, you can just search for substrings.
var anchor = url.substring(url.indexOf("#"));
var gets = anchor.substring(anchor.indexOf("?"));
Although, I would like to point out that your url is incorrect. The anchor should go after the get variables.

Get URL variable in PHP

I m in a situation where i am redirecting user to another page with following jQuery code
window.location = "/#/customer/email?isEmail=true&eid=1&template=2";
i have some url re-writing , and so complete url becomes is
https://demo.qa.com/#/customer/email?isEmail=true&eid=1&template=2
but in PHP when i try to get full page url using this
echo $_SERVER['REQUEST_URI'];
it just gives me this
/
i just want to get variable IsEmail
$_GET['IsEmail']
value in PHP page,
I think the
#
in between the URL is creating the problem, is there any way to get it, please advise..
The fragment is never sent to the server, so if you want access to the query parameters you need to bring them forward:
https://demo.qa.com/?isEmail=true&eid=1&template=2#/customer/email
^ ^
query fragment
The anchor fragment portion of the URL (anything after #) isn't sent to the server at all. It only lives client-side. The server has no knowledge of it, and therefore PHP has no knowledge of it.
If you want to do anything with the anchor fragment, you must do it client-side.

$_SERVER['REQUEST_URI'] returns PHP file not URL

Below are the relevant lines of code:
update.js
$('#tablePlaceholder').load('updateTable.php');
updateTable.php
$path = $_SERVER['REQUEST_URI'];
Normally $_SERVER['REQUEST_URI'] returns the URL, however in this case, the URI at the moment of the request is technically the PHP file, so what is returned is updateTable.php instead of the actual URL. Is there an alternate command I can use to get the URL instead of URI? I have resorted to storing the URL in a session/global variable to retain access to the actual URL in the PHP file, but it seems there must be a better way, though I have not found it. Any tips would be most welcome!
I was looking at the documentation for the jQuery load() command:
http://api.jquery.com/load/
It looks like I might be able to pass the URL as a string via the data argument?
$_SERVER['REQUEST_URI'] is supposed to give path of current script relative to root and not the full url. If you want to get full url path, take a look at the same question on stackoverflow with the solution also being there
Getting the full URL of the current page (PHP)

GET method in php clean URL

I have successfully created clean url for my project.. now what i need to do is add variables to URL..
localhost/user1/file?action=add
localhost/user2/file2?action=delete
where user and file are already rewritten i dont want it to be like localhost/user/file/add because localhost/user/folder/file will be mistaken to to the action parameter.. please help
Try using the ampersand instead of question mark:
localhost/user2/file2&action=delete
In your htaccess, the rewrite rule might look something like this:
RewriteRule ^user([0-9]+)/file([0-9]+)$ /page\.php?user=$1&file=$2
As you can see, the question mark is already there even though it is masked in the address bar. Appending another variable to the query string would require the ampersand for successful concatenation.
You need to get the url and start parsing the url from the question mark. I would save the contents then to an array, so that you've got a key and a value.
$uri = $_SERVER["REQUEST_URI"];
$questionMark = explode('?',$uri);
$questionMark[1] is the action=delete then. There are probably better ways then using explode() method here, but I just wanted to show how you get the string.
You can read GET variables in PHP by accessing the global $_GET array:
http://php.net/manual/en/reserved.variables.get.php
In your example, the php file that is used for handling files would be able to read in:
echo $_GET['action']; // 'add' or 'delete'

Categories