I'm having some problems passing URL's as GET parameter. When I try to access:
http://www.linkebuy.com.br/linkebuy/parceiro?url=http%3A%2F%2Fwww.google.com
I get the following message:
However, if I go for:
http://www.linkebuy.com.br/linkebuy/parceiro?url=123
Everything works just fine (it redirects to an inexistent site - 123 -, of course, but it does the expected). By elimination I can say there's something wrong with the url parameter, but what is it?
OBS: I'm using rawurlencode() to encode the URL.
EDIT: Code you asked...
In the first view, where the link is (http://www.linkebuy.com.br/notebook/detalhe?id=5):
<!-- url() function just completes the right URL (production or development) -->
<a href="<?php echo url('linkebuy/parceiro/?url=' . rawurlencode($l->getUrl()), true) ?>" class="<?php echo $leadClass ?> oferta" target="_blank">
<?php echo $l->getNomeFantasia() ?>
</a>
When clicked the link redirects to an action (/linkebuy/parceiro), where happens the following (basicly nothing, just keeping in the framework):
public function execute($request, $response) {
$response->addParameter('url', rawurldecode($request->getParameter('url', ''))); //This creates $url in the view
$response->setTemplate('site/linkebuy/lead-parceiro.php'); //Forwards to the view
}
It includes the view, lead-parceiro.php (above on the question, I link to this page), where the head contains:
<script type="text/javascript">
setInterval(function(){ window.location = '<?php echo $url ?>'; },3000);
</script>
If you can't get rid of the restriction you can pass the url in 2 parts like this
http://www.linkebuy.com.br/linkebuy/parceiro?protocol=http&url=www.google.com
And then parse it on your code to make the full url for the redirect.
You should use urlencode when you pass anything as URL parameter
Related
I am installing Disqus Comment system on my website. I understand that I can dynamically call the page url or id via php and thus do not need to hardcode the URL or page ID everytime I have a Discus comment box.
This question asked 7 years ago this answer was provided by #Yogus but I couldn't get it to work.
<HTML>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
include 'testConnection.php';
?>
<b>Here is some more HTML</b>
<?php
//more php code
?>
</body>
</HTML>
I've hear that to use PHP I must change my website pages to end in .php rather than .html. I don't want to do this for every page as it would probably affect SEO and I've currently 325 pages.
I'd like to do a call to a PHP file, have it return a variable with the URL or Page ID and then I can plug this into the Discus. I've read there is a dedicated variable maybe $_SERVER which returns the page URL or ID.
Any help would be appreciated! Oh, a link to one page where I've Discus installed is here {go to bottom}. This page is hard coded in.
coinsandhistory.com/countries/Ancients_Rome/Ancients_48_Rome_Empire_Tetrarchy-Constantine.html
You can still keep your pages as HTML in this case.
You can make a call to the PHP page with Javascript, get the result via AJAX, and then use that result in a URL. In this example, I will feed the php a parameter that will determine what the URL will be. The HTML page will use javascript to populate the link in the anchor tag with the data coming from the php file.
example
the PHP code would look something like:
<?php
$p = $_REQUEST['p'];
if($p == 'one')
echo 'http://urlone.php';
if($p == 'two')
echo 'http://urltwo.php';
?>
the HTML code would look something like:
<html>
<script>
function populateURL(param){
//get the anchor tag
var link = document.getElementById('dlink');
//call the php file to return the url as a string,
//then set the url of the anchor to that string
fetch('url.php')
.then(response => (response){dlink.href = response});
}
//get the url if the value is 'one'
populateURL('one');
</script>
<body>
<a id="dlink">CLICK HERE </a>
</body>
</html>
I found a working answer on SO from ~10 years ago.
Get current URL with jQuery?
The solution was to use javascript from within the HTML which has queries for such things.
// Returns path only (/path/example.html)
var pathname = window.location.pathname;
// Returns full URL (https://example.com/path/example.html)
var url = window.location.href;
// Returns base URL (https://example.com)
var origin = window.location.origin;
To look at the results I just use the javascript to write out the answer, i.e
<p>JavaScript variables<br><br>
page url:
<script type="text/javascript"> document.write(page_url)
</script>
Thus php nor an external file wasn't needed. A note the javascript variable assigns MUST be done before the print commands, otherwise Chrome reports an error & nothing is displayed.
Okay, so I have two pages, home.php and page.php. On home.php, I load page.php onto it using jquery. In the url of home.php there is a GET value, u. I want to access that GET value on page.php. You can see that I tried the regular $_GET method, but this does not work. If on page.php I can just get the url of home.php with the GET variables in the url, I can get the info from there. But right now I cannot get the url of home.php, it just gets page.php. I hope that makes sense and thanks for the help!
Home.php
<div id='holder'></div>
<script>
$('#holder').load('page.php');
</script>
Page.php
<?php
$u = $_GET['u'];
echo $_SERVER['PHP_SELF']; // echo's "page.php". I need it to echo "home.php?u=test"
//some php
?>
What you can do is pass the $_GET variable to the requested page.
Do it like this:
<div id='holder'></div>
<script>
$('#holder').load('page.php<?php echo "?u=" . $_GET[" u" ]; ?>');
</script>
Stackoverflow doesn't parse it the way I want but I'm sure it works.
I have written a small script go.php that takes the link parameter from the referring url and then redirects the user after a few seconds to that page.
For example the refferring url is:
http://www.site1.com/go/go.php?url=http://www.site2.com
The go.php script includes the following code:
<script type="text/javascript">
function movetopage(){
window.location = "<?php echo $_GET['url']?>"
}
</script>
<body onLoad="setTimeout('movetopage()', 3000);">
This works absolutely fine if the url I'm sending people to is a standard domain name.
But I want to refer it to an affiliate offer, and that affiliate offer also redirects in the form of:
http://site1.com/go/go.php?url=http://www.site2.com/redirector.aspx?aid=6352
Something is wrong, when go.php initiates the movetopage function to send them to the next link it returns a 404 and I'm guessing this has something to do with the second redirect.
Can someone please tell me how I can adjust the code to get this working, thanks!
What if you just jump to it with PHP?
if(isset($_GET['url']))
header('Location: '. $_GET['url']);
I have 3 links on page index.php:
<div id="languages">
<a href="index.php?page=test&lang=en" id="flag">
<img src="./images/flags/en.png" class="flag_off" alt="en" title="en">
</a>
<a href="index.php?page=test&lang=sk" id="flag">
<img src="./images/flags/sk.png" class="flag_off" alt="sk" title="sk">
</a>
<a href="index.php?page=test&lang=cz" id="flag">
<img src="./images/flags/cz.png" class="flag_on" alt="cz" title="cz">
</a>
</div>
each of them is passing lang parameter and on same page (index.php). Then I have variable
$_GET['lang']
which will call function for changing language:
$obj->change_language($_GET['lang']);
Q: How can I do this by using jquery get() function without showing parameter in url of index.php page?
(Pass variable $lang through jquery)
I don't like URL with parameters after calling function for example:
index.php?page=test&lang=sk
I suppose using .click() function as handler.
EDIT:
I have something like this:
$(document).ready(function(){
$("#flag").click(function(){
$.get('index.php',
{ lang: "VARIABLE LANG FROM HREF" }
);
});
});
How to parse sk from href index.php?page=test&lang=sk part ?
If your problem is the parameter in the URL, you could try setting it with Ajax (http://api.jquery.com/jQuery.ajax/) and than reloading the page. I don't think you could get around refreshing since your content should be changed to the selected language as well?
$(".flag").click(function(event) {
event.preventDefault();
$.get(this.href, function(data) {
location.reload();
});
});
You don't need jQuery at all do to this. You can do something like
<div id="languages">
<a href="changeLanguage.php?lang=en">
<img src="/flags/en.png" alt="en" title="en">
</a>
<a href="changeLanguage.php?lang=sk">
<img src="/flags/sk.png" alt="sk" title="sk">
</a>
<a href="changeLanguage.php?lang=cz">
<img src="/flags/cz.png" alt="cz" title="cz">
</a>
</div>
Then, in this changeLanguage.php, you do your $obj->change_language($_GET['lang']); (where you should check/cast the value of the lang parameter), then, you can redirect the user to the referer page with something like
$url = (string) $_SERVER['HTTP_REFERER'];
if (0 === strlen($url)) {
// redirect to the home page if there is no referer
$url = '/';
}
header('Location: '.$url);
This way, you don't need any Javascript code to this. I think this is important in some ways:
you only do one HTTP request instead of two with the AJAX solution,
when the user click, the browser reacts immediately: the request is sent right after the click so the "loading icon" of the browser is showing that something is happening
this effect of action->reaction is something important in the User Experience, with the AJAX solution you have to wait for the first request to finish to trigger the "loading icon",
the location.reload() used with the AJAX solution can show up a dialog like Would you like to submit the form again ? if the user has arrived to this page with a POST request, and he might not understand what's the relation between its action (change the language) and this "error" message, which leads to a degraded User Experience too,
if your Javascript is broken is some way, those link might not work at all if your page to change the language only change the language. Let's say the page index.php?page=test&lang=en is doing something like $obj->change_language($_GET['lang']); exit;, because this page's purpose is only to change the language. Then if the Javascript code is not triggered for any reason (the code is broken, handlers are not bound, the user open up the page in a new tab), then the page loaded will be a blank page. And if you handle this case, then the "change language page" would look like a normal page, that's ok but it means that you'll do two HTTP requests with a "complete" response (one for changing the language, one other after the location.reload), and this leads to the first point,
use Javascript only where it's needed :)
What do you think ?
Dear all
I have database of website link, it list out in main file ,when i try to click that link it get to redirect on that database link.
my code is:
file: test.php
<?php
// getting from database
echo '<li onclick=\"window.location='.$result->website.'\">
'.$result->option.'</li>';
?>
The Main.html calls that test.php
while ajax
$.post("test.php", {queryString: ""+inputString+""}, function(data){
});
how to do it?
any idea Is it possible with serverside script? whats wrong with my php code?
Your code does not work for invalid URLs. www.google.com is not an URL, just a domain name. So skip the silly Javascript links, instead use:
echo "<li>$link</li>\n";
And your Javascript success function seems somewhat empty, so instead use .load() like:
$("ul").load("links.php", {queryString: ""+inputString+""})
Edited after venkat's comment:-
According to your last comment, the code you have problems with is the following:-
<?php
$link="www.google.com";
echo "<a href='#' onclick=window.location='$link'>Click here</a>";
?>
This above code should actually be the following:-
<?php
$link = "http://www.google.com/";
echo 'Click here';
?>
The reason for adding the "http://" string is that the variable "$link" is going to be used as an HTTP URL, which requires mentioning of this "http://" string, mainly because of the protocol to be used by the browser. In this case, the protocol is HTTP.
Always remember that for any URL, there must be a string "http://" at the beginning of the URL string, when stored in either a database / variable.
Coming back to the code in your question, which was:-
<?php
// getting from database
echo '<li onclick=\"window.location='.$result->website.'\">'.$result->option.'</li>';
?>
Now here the position of "window.location" is not totally correct. It should have been in "href" attribute of "a" element, instead of putting it in "onclick" attribute of "li" element.
So the code should actually be:-
<?php
// getting from database
echo '<li>'.$result->option.'</li>';
?>
Hope it helps.