Pagination Links Not Directing Properly - php

I'm new to PHP, and I'm developing a PHP plugin for CraftCMS.
On my local dev machine, this code was working; however, on the server, the pagination links are not directing properly.
This is a plugin which imports an XML feed and paginates the results.
For example, when using 'PHP_SELF' in the code, the link redirects to the index page, and when using 'REQUEST_URI,' the link works correctly the first time, yet is appended with each successive URI.
I was wondering if there would be a work around or better method for correcting this problem?
This version takes you back to the home page:
code:
echo '<< < ';
while this version appends the querystring each time:
e.g. http://mypage/test?page=1&page=2
code:
echo '<< < ';

Here is what is happening
The first time you use $_SERVER['REQUEST_URI'] the URL on the address bar (which is the value returned by $_SERVER['REQUEST_URI']) is http://mypage
Now say you click on the "First" button, you get redirected to $_SERVER['REQUEST_URI'].'?page=1#rss' so now your URL on the address bar reads http://mypage?page=1#rss
So now what happens when you click on "First" again? The text ?page=1#rss is appended to the already existing request Uri i.e http://mypage?page=1#rss. So the result is? You guessed it
http://mypage/test?page=1#rss&page=1#rss
To avoid this, you need to strip the querystring (the part following the ?) from the request Uri and there exists a wonderful SO answer to help you do just that
So your code should look like
$url=strtok($_SERVER["REQUEST_URI"],'?');
echo '<< < ';

Related

Redirect a web page URL to another website

I have a simple PHP website. Let's call it youtubex.com. I want to redirect youtubex URLs (in the format shown on STEP2) to my website in the format shown on STEP3. Here, I am using YouTube, just for illustration.
STEP1: https://www.youtube.com/watch?v=lj62iuaKAhU
STEP2: https://www.youtubex.com/watch?v=lj62iuaKAhU
STEP3: https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
STEP1 shows any desired URL. STEP2 shows the same URL from STEP1 with youtubex as domain. STEP3 shows the final required URL. I am trying to redirect STEP2 to STEP3.
I tried finding some solutions to this on the internet and SO, but, none help. Here is one.
This should do the work:
RedirectMatch 301 /watch$ https://www.youtubex.com/#url=https://www.youtube.com/watch
An inefficient but full php solution can be using the location header in php :
$vid = $_GET['v']
if($vid){ header("location:https://www.youtubex.com/#url=https://www.youtube.com/watch?v=$vid");
}
by the way you can't get the text after the hash mark in php because it is not sent to the server.
javascript can do it in a more neat way without the second reload by checking window.location.href to see if the hash does not exist already and then get the v parameter in url then change url without refreshing the page by using window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
using php str_replace and header :
$step2_url = "https://www.youtubex.com/watch?v=lj62iuaKAhU";
$part2_url = str_replace("youtubex","youtube",$step2_url);//the output is : https://www.youtube.com/watch?v=lj62iuaKAhU
$step3_url = "https://www.youtubex.com/#url=".$part2_url; //the output is : https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
now you have the final url , simply redirect
header($step3_url);

URL being re-encoded?

I have a few links that look like this:
https://www.example.com/find?category=food%20%26%20drink
Clicking on the link should take me to a page where I can GET the variable, and it SHOULD read "food & drink".
However, when I click the link, it takes me to this url instead:
https://www.example.com/find?category=food%2520%2526%2520drink
the variable reads: food%20%26%20drink.
If I paste the first url into the search-bar directly, it works fine. But if I click on it as a link, then it gets re-encoded somehow.
Any idea how to get it to read "food & drink" even though it comes from a different page?
many thanks in advance!
Realized the links were written as http instead of https.
Consequently, they were being re-written by the htaccess file to https when clicked, and also being re-encoded at the same time.
The link you have is double encoded. The possible solution to this would be
Find line of code where the link getting encoded again and make suer not encode if encoded already. Couple of examples are given here Click Here
If there is no way you can change the code form where the URL is getting generated, then you have to use urldecode twice to parse the url params
<?php
$query = "https://www.example.com/find?category=food%2520%2526%2520drink";
$param = explode("=", $query);
print_r(urldecode(urldecode($param[1])));
?>
Hope this helps!

external link sends me internally

I am have a form that requests a user to submit a website and then on a different page I send a mysql query to retrieve that website an and turn it into a link by doing this in PHP (V=5.6)
$link = '' . $school_website . '';
the problem is that when i try to click this link, instead of sending me to www.google.com for example, it directs me to www.mydomain.com/www.google.com.
I fixed it originally by using substr to see if the first part was "www" and if it was adding "http://" to it and it worked but now i realize that not all websites will start out with that.
I have tried googling this problem but Im not quite sure how to word the problem so I am not getting anything.
My apologies if this is a duplicate. I did not see anything here that fits my problem, so any help would be greatly appreciated. Thanks.
Could always check if it has http/s:// with regex, if it hasn't then add http:// and the link will work as it should. Or make it ugly but simple.
Simplest way is to remove any protocol and prepend // - that would mark the link as absolute and adopt your current protocol. Even if it didn't have http/s:// it would work as it should.
$school_website = '//' . str_ireplace(['https://', 'http://'], '', $school_website);
Example:
https://google.com becomes //google.com
google.com becomes //google.com
www.google.com becomes //www.google.com
In any of the above cases it would become a absolute url.
A better but longer way would be to validate the url with regex.
Until you add http:// or https://in front of url. It will remain the relative
Like if you re on www.mydomain.com and your href attribute value is www.google.com, The attribute value remain the relative and will target to
you url.
You need http:// or https:// at the beginning of the URL of external links - in fact that should be part of your variable "$school_website", and if that one is for example retrieved from a database, that value has to be changed in the database.

How can I stop myy query in URL is from being lost after loading the page

I have an affiliate program and have a serious problem that I can figure out.
My affiliates have links like this...
http://example.net/?p=14&ref=delta88
Once the page loads it changes to...
http://example.net/?p=14
Which totally gets rid of the ref id. I need it to keep the whole URL in the bar in case they hit refresh. Because when you hit refresh it takes the affiliate out of the system and just let's people join without an affiliate.
The way my code works for the pages is this...
That URL goes to an index.php file. In that file it finds all the affiliates information. It then uses an include to show the page. So it's not pointing directly to the page. I need to use the include because I store about 27 pieces of data in strings and I can't put that information in a URL as queries and have it forward to that page.
I added that information because it may be because of the include that's causing it and that will help you better figure out a solution for me.
Use a SESSION, its like a variable that holds for each user, here is a tutorial but works like:
<?php
session_start();
if(isset($_GET["ref"]){
$_SESSION["ref"] = $_GET["ref"];
}
?>
Now, in any PHP that open the user, will have that variable set ( $_SESSION["ref"])
you can keep current url in variable , see below used actual_link to hold the current url data.
if($_GET){
$actual_link = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//if you want to redirect page on same url just call header with $actual_url
header('Location: '. $actual_link);
}

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.

Categories