urlencode and GET request breaks at Ampersand - php

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

Related

Send URL as a Query String

Think my current page url is:
http://example.com/id=10
In this this page has link to go other page, I want to pass current URL as a query string like this:
http://example.com/about-us/?edit=1&return=http://example.com/id=10
in PHP
http://example.com/about-us/?edit=1&return=<?php echo $_SERVER['QUERY_STRING'] ?>
but this is not working, could anyone help me to do this.
Use this (I assume you are using http only);
$currentUrl = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
$link = "http://example.com/about-us/?edit=1&return=" . $currentUrl;
use urlencode($_SERVER['QUERY_STRING'])
It encodes the link.

How to pass a hash tag (#) with an address bar variable ?searchTerm=#JustKeepGoing

How can I pass a variable that contains a hash tag via a website URL?
I currently pass this information into the URL, but cannot echo it out on the new page because '#' is used to anchor/jump to a specific page area.
Can I tell it to ignore the hash tag and echo what follows it?
Current url:
http://localhost/social3/search/?searchTerm=#JustKeepGoing
Current way to echo content:
$searchTerm = $_GET["searchTerm"];
echo $searchTerm;
You have to url encode it :
http://host.com/social3/search/?searchTerm=%23JustKeepGoing
Not sure your browser won't interpret it, but worth trying.

Remove string from the link href

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;

URL Redirect, HTML, PHP

I want to link links on my website via go.php?urlhere, I have been told and I have tried using go.php?url=urlhere however the URL's to which I redirect, redirect to another URL with-in it for example go.php?http://.com/click?p=0&a=0&url=http://.com, many of the redirect I have tried to use simply copy the URL in the go.php file and use a meta refresh or a window.location reload; however they redirect to the second URL and not the first one. Sometimes when I do actually get it to redirect the first part of the redirected URL gets all the dots changed to "_" which stops it redirecting.
I want to have something like this website using on its "Buy It Now" buttons
http://www.searchchief.co.uk/Search/Sony-PSP-Go
So I think this is what you are asking: How do I redirect to a page using a url as a query string parameter?
eg.
http://www.myurl.com/go.php?url=http%3A%2F%2Fwww.myotherurl.com
You must do two things to achieve this on the url:
have a query string parameter. eg.
go.php?url=mysite.com or
go.php?redirect=mysite.com not
just go.php?mysite.com.
you must URL ENCODE this query
string parameter value. eg. go.php?url=http%3A%2F%2Fwww.myotherurl.com
NOT go.php?url=http://www.myotherurl.com. In php you can use the urlencode() function or http://meyerweb.com/eric/tools/dencoder/ can do it for you if you have a small number of urls to convert.
On the PHP side of things you will use the following
<?php
header('Location: '.urldecode($_GET['url']));
exit();
?>
You can do other basic checks on the php side of things eg. check for a valid url format etc
I think you want to use the header command.
So, in your case you would do this:
For a url in this syntax: go.php?url=thisurl
This would be the code
<?php
header('Location: ' . $_GET['url']);
?>

How can I pass the correct variables in a URL in PHP for my paging?

I am working on paging in PHP
I need it to work on any folder so
root/ root/folder1/
root/folder1/folder2/ would all work
It should pass the page number
variable for the script to work in
the URL but also retain any other
variables in the URL that may be
present with any amount of variables
passed in tthe URL
Here is what I have so far for the link based on being #
http://domain.com/test/paging/index.php?var=cool&var2=coo2l&page=1
$selfurl = "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF'])."";
This would create this: http://domain.com/test/paging/
$qry_str = $_SERVER['argv'][0];
This would create this: var=cool&var2=coo2l&page=1
So far so good, it works no matter how deep the directory system is and passes ANY and ALL variables that may exist in the pages URL
(NOTE; I know it assumes it is always the index file I will fix that later)
So all of these links below would get passed through my paging links;
http://domain.com/test/paging/?page=1
http://domain.com/test/paging/?var=coo2l&page=1
http://domain.com/test/paging/?var=cool&var2=coo2l&page=1
Here is the problem, below is an
example of my NEXT and PREVIOUS link, it works
but the problem is after I go to the
first page, it will keep adding
&page=THE-NUMBER-HERE so when you get to page 3 it
would be like
&page=THE-NUMBER-HERE&page=THE-NUMBER-HERE&page=THE-NUMBER-HERE
instead of
&page=THE-NUMBER-HERE
The links;
Next
<a href="<?PHP echo $selfurl;?>/?<?PHP echo $qry_str;?>&page=<?PHP echo $start-$pagesize;?>" class="bluelinkbold" >Previous</a>
How can I resolve this path correctly?
it's simple:
don't use $_SERVER['argv'][0], construct URL by yourself on every request.
use preg_replace to remove existing page variable from $_SERVER['argv'][0] then add new one:
$url_without_page_var=preg_replace('/page=(\d+)/i','',$_SERVER['argv'][0]);
I had a similar problem when passing a page number after an id variable.
mysite.php?id=16&page=1&page=2&page=1 and so on...
Inspired by Sergei, I remove the existing variables using PHP_SELF, then reload my id and get the new page at the same time.
href="' . $_SERVER['PHP_SELF'] .'?id='. $row['id'] . '&page=' . $pages->next . '">Next

Categories