Remove string from the link href - php

I'm using a jQuery pagination script and I'm using the onChange function so if a user click on the page number it does redirect him to the $_SERVER['REQUEST_URI'] + it adds an page number to the request url, but if I will click on some pages several times then the request url looks like this: &page=3&page=1&page=10 ... etc.
The code looks like this:
onChange : function(page){
window.location = '" . $_SERVER['REQUEST_URI'] . "&page='+page;
}
Now I need to remove $page=??? from the url if it already exist.

After this
$url = $_SERVER['REQUEST_URI'];
$url = preg_replace_all("/\\&page=[^\\&]+/", "", $url);
$url will contain the url barring the page attribute

The reason for this is that every time the user clicks on your link, the value of $_SERVER['REQUEST_URI'] is the current URL and you are just appending an extra string to the end.
You need to set the get variable to the page you want then just change this variable when your function is called. Something like:
$_GET["page"] = page;

Related

PHP header appends to old url

I am trying to implement a feature where after a button is clicked, the server redirects the user to a new webpage with some variables in the url. I am running into a bug where the header function keeps appending to my existing url instead of refreshing it. EX:
http://localhost/455Project/messaging.php/new_message.php/?ssn=
messaging.php contains a button which when clicked redirects to new_message.php with a ssn attribute.
var_dump($_GET);
$temp = $_GET['patient'];
if(isset($_GET['new_message'])) {
$url = "new_message.php/?ssn=" . $temp;
header("Location: " . $url);
die();
}
The current url has the attribute which I want to get passed to the next. The var_dump has the correct value. I have also tried using "/455Project/new_message.php/?ssn=" which solves the url appending but in either instance the $temp variable doesn't append the proper value.
EDIT: I have a dropdown that produces the url like this echo "
`href='messaging.php/?patient=$patient_ssn_row[0]'>$name_row[0] $name_row[1]'</a>";`
This corresponds to a dropdown with data and the user selects one of the options.

Get URL by $_GET

I need get current url (url can be different), I want send this url to email by ajax form, but I cant get right url. I use:
echo $_GET['ref']
But it return empty value.
Addition:
I have ajax form which send some data (including current url) to my email.
Yes, I use this 2 values:
$url1 = $_SERVER['HTTP_HOST'];
$url2 = $_SERVER['REQUEST_URI'];
But it return me url like this:
/cloud/abuse/abuse_mailer.php
not my current URL.
$_GET is an array containing data passed through a GET request method. If you want to get your current URL, then you can use $_SERVER; it contains server and request data. As an example:
echo $_SERVER['REQUEST_URI']; // outputs current URI of client
Or you can try:
echo $_SERVER['PHP_SELF'];

how to take the link with its variables from the page

how can i take the link of the page that i am on with its variables ?
example i have the page link is
article.php?article_id=10&article_title=title&lang=ar
when i use the $_SERVER['SCRIPT_NAME'] variable it takes only article.php
and im rewriting the url as well so it looks like this
article/10/title/ar
what i want to do is just make a link that is to an English page so im trying to make it look like this
article/10/title/en
how can i do that?
Since the data looks like its passed with a HTTP GET method, you can use this
$_GET["lang"];
This returns the value assigned to "lang"
$_SERVER['QUERY_STRING'] will have all the parameters. You can also check $_SERVER['REQUEST_URI'] and that should contain the whole url, file and parameters.
Something like:
$params = $_GET;
$params['lang'] = 'en';
$link = basename($_SERVER['SCRIPT_NAME']) . implode('/', $params);

urlencode and GET request breaks at Ampersand

I am working on a wordpress website which has thousands of pages and the owner has entered an affiliate link for each page via a custom field named: afflink
The affiliate link is outputted on the page using:
<?php echo get_post_meta(get_the_ID(), 'afflink', true) ?>
The user clicks the link which sends them to a page called go.php
The link looks like this:
www.mysite.com/go/go.php?url=http://www.somesite.com/redirector.aspx?aid=334&cid=2502&tid=3
Within the go.php page is the following meta refresh tag:
<meta http-equiv="refresh" content="5;<?php echo $_GET['url']?>
" />
However, when the page refreshes it sends us to just:
http://www.somesite.com/redirector.aspx?aid=334
How can i fix this?
You should use urlencode before printing link to the user, not after he clicks the link:
$link = "http://www.somesite.com/redirector.aspx?aid=334&cid=2502&tid=3";
echo '' . $link . '';
[+]
I strongly recommend writing some script that will change existing entries with proper ones. If all of them starts with www.mysite.com/go/go.php?url= then you can replace it with nothing in database, add this part to your meta tag and echo urlencoded link from db.
Any other solution will be just a kludge. One of it is to recreate original url from the rest of GET parameters in go.php:
$url = $_GET['url'];
unset($_GET['url']);
if ($_GET) {
$url .= '&' . http_build_query($_GET);
}
You're misusing URLs.
Your URL is parsed like this:
Path: go/go.php
?
First query string argument: url=http://www.somesite.com/redirector.aspx?aid=334
&
Second querystring argument: cid=2502
&
Third querystring argument: tid=3
Instead, you need to URL-parameter-encode the inner URL.
No need to urldecode a GET or REQUEST variable, they are automatically decoded:
http://php.net/manual/en/function.urldecode.php

How can I add GET variables to the end of the current page url using a form with php?

I have some database information that is being shown on a page.
I am using a pagination class that uses the $_GET['page'] variable in the url. When you click on a different pagination anchor tag, it changes $_GET['page'] to a new number in the url and shows the corresponding results.
I have sort and search features which uses the $_GET['searchby'] and $_GET['search_input'] variables. The user enters their search criteria on a form that is using GET. The variables are then put into the url allowing for the correct results to be shown.
The problem I am having is that whenever I click on a pagination link, it adds the $_GET['page'] variable to end of the url and erases $_GET['searchby'] or $_GET['search_input']. When I submit the search form, it adds $_GET['searchby'] and $_GET['search_input'] but erases $_GET['page'].
How can I add GET variables to the end of the current page url using the anchor tag and search/sort form without having it erase any existing GET variables, but overriding them if they're the same GET variable name?
Try this:
if (strpos($_SERVER['REQUEST_URI'], '?') !== false)
{
$url = $_SERVER['REQUEST_URI'] . '&var=value';
}
else
{
$url = $_SERVER['REQUEST_URI'] . '?var=value';
}
Go
Note that $_SERVER['REQUEST_URI'] gives you current url including query string value.
$query_string = http_build_query(array_merge($_GET, array('page' => $page)));

Categories