Get Query String URL Path in PHP with $_SERVER - php

I have a form that has a "textarea" where I'm looking to capture the full URL of the page the user is on. Once this form is submitted I'm looking to include the full URL into an email that goes out.
However, the problem I keep running into is that the string gets cut off at the first "&" in the URL string.
Here is an example URL string:
example.com/folder/results.php?lat=38.10591&lng=-51.556916898&zipcode=32827&countryIso=US
Here is what I have tried:
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>
I've also tried:
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['QUERY_STRING']; echo $url; ?>
Neither one worked to include the full URL... Is there something I'm missing?

Unless you send your form's POST request to this url :
example.com/folder/results.phplat=38.10591&lng=-51.556916898&zipcode=32827&countryIso=US
I think your problem is that you are logging the url of the url of the file processing the form. Have you tried storing the content of this into an hidden field of your form?
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>

Related

How do I redirect using "xyz snippet" in a wordpress site

I am using xyz-snippet to insert some php code (post forms and handling post requests) in some Wordpress pages. In the work I am doing it is necessary to take input from a user (with a post form) and process it and redirect them to another page depending on their input. I tried the header() function and it didn't work now I am trying this:
if (empty($entry)){
echo('<h1>I am empty and should be redirecting now</h1>');
$url = 'https://jimmyshapopi.com';
wp_redirect($url);
exit;}
This is not working. I know my logic is fine because that <h1> tag actually prints but the redirect is not working.
XYZ PHP Snippet, uses shortcodes for you to enter you php code into a page. What am I missing?
Thank you to #Sysix and #user3783243 for the answer.
The workaround is to use JavaScript. Echo some JavaScript that changes the window.loaction. The JavaScript code is:
window.location.href = url
Since this needs to be written in the context of a PHP script you need to echo a <script> tag like this:
echo '<script>window.location.href= "' . $url . '";</script>';
In this context the $url variable is defined in the PHP code and not in the JavaScript.

How to pass URL in URL (as GET parameter) using PHP?

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

How can I capture/pass a keyword through a URL and pass it to a PHP page

I'm looking to pass a keyword from a HTML page to a PHP page through the URL string.
See example below to what I'm saying:
mysite.com/index.html?keyword=Google
the "Keyword" is Google and it needs to go over to my Thank you page.
mysite.com/thankyou.php
the code I use on my "thankyou" page is:
<?php $id = $_GET['keyword']; echo $id;?>
NOTE: I've visited and tried the links I found on the stackflow site... but none of them seem to work (asleast from what I've done with them). Am I missing any other thing when it comes to the code?
How do I get the current URL and then get the last words
how can i get the url if it is like this <keyword>?
How to pass a keyword from URL but keep it hidden? (Adwords)
If you could PLEASE provide a functioning example on a http://jsfiddle.net page so I can see how it works.
You need to have the get variable on the php page url, not the index.html page.
Example:
mysite.com/thankyou.php?keyword=Google
Alternatively you could write a html <form> and make a <input type="hidden" name="keyword" value="Google"> and grab it from POST instead of GET with PHP.
<?php $id = $_POST['keyword']; ?>

Pass complex URL as parameter in PHP

I'm trying to pass a complex URL as a url parameter but the problem occurs if the url contains & for example I want to pass the following link as a parameter
http://www.google.ps/search?hl=en&client=firefox-a&hs=42F&rls=org.mozilla%3Aen-US%3Aofficial&q=The+type+%27Microsoft.Practices.ObjectBuilder.Locator%27+is+defined+in+an+assembly+that+is+not+referenced.+You+must+add+a+reference+to+assembly+&aq=f&aqi=&aql=&oq=
I'm trying to get a URL as a parameter from a user and redirect user to this URL.
How could I handle this in PHP?
The whole Story:
I'm trying to make some ads analytics on flash files so user submit flash ads to a website which contains a link to the required webpage.
Now,my client needs to know how many times this flash file was clicked.To solve this I 'll till every one who submits flash to write a link to my client webpage and pass the required URL as a parameter as follows
http://myclientwebpage.com/disp.php?link=www.google.com&id=16
by this way I can update my database and get a count for how many times this link was clicked
Use urlencode() or rawurlencode().
You wrote:
I'm trying to get a URL as a parameter
from a user and redirect user to this
URL.
But you didn't answer the question - how do you get that URL? How does user provide you with it? Is it written in <input type='text'/>? Or does user click some link that contains URL as one of parameters? Or is passed as ID of some URL that is stored in DB?
One case that comes into my mind - replacing URLs in plain text and sending user to some "redirecting page" before opening real page, so final page does not see HTTP referrer which might contain some secure data (e.g., session ID). In this case, you would write
<a href='redirect.php?link=<?php echo rawurlencode($url); ?>'>
<?php echo htmlspecialchars($url); ?>
</a>
instead of
<a href='redirect.php?link=<?php echo $url; ?>'>
<?php echo htmlspecialchars($url); ?>
</a>
From what I understand, you'll need to replace & into &.
$url = str_replace('&', '&', $url);
& is reserved, used for separating GET parameters, & is for writing a literal ampersand in a URL.

link Redirect from php

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.

Categories