link Redirect from php - 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.

Related

Javascript function to return PHP get_file_contents

I am stumped here, have asked this a few different ways. What I need to do is return the contents of a text file in PHP on click of an a tag. I thought it would be easy enough to just have the click and ID of the anchor tag in the JS and put the PHP method in the template like so. $textPath is var that points to the file in the settings. It goes to a help section and the text file authenticates the user. So the UL ends up being www.blahblah.com/help
<li><a id="help" target="_blank"><? echo file_get_contents($textPath);?> </a></li>
//Simple JS (part of another 'open' function for the menu)
$('#help').attr("href", "/help")
But it's still not working at all. Any ideas? Thanks
Highlighting a part of your code here:
<a id="help" target="_blank"><? echo file_get_contents($textPath);?> </a>
See you are using the shorthand echo tag. The opening tag should be <?= insead of <?. And since this is a shorthand echo tag, you don't need to use the echo construct.
This shall work fine:
<a id="help" target="_blank"><?=file_get_contents($textPath);?> </a>
Quoting from the PHP Manual
echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
I have <?=$foo?> foo.
Coming to the question, you want to return the contents of the file. Your best bet will be achieving this via AJAX.
You can output as you wish. The best way will be to make a separate php file for handling all the ajax calls and return the output as plain text. To authenticate the user, just include the auth.php file(assuming that you are using one) in the ajax.php page.
ajax.php
<php
//Below code checks if a GET request is sent
if (isset($_GET['path'])) {
echo file_get_contents($textPath);
}
?>
jQuery
$('#help').click(function(e) {
e.preventDefault(); // Prevents the default action
// Send an AJAX request
$.get("ajax.php?path", function(s) {
//s is the responsetext. It contains the response from the server
your code here
});
});

Get Query String URL Path in PHP with $_SERVER

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; ?>

Change Location of Outgoing Link On Click

I'm trying to link to another page without displaying the real URL on hover or in the source code. I want <a> to redirect to $newUrl; even if there is another url defined in the href="".
<?php $newUrl = "http://therealurl.com/folder/private.php" ?>
Link
So using the code above I want to replace http://domain.com with <?php echo $newUrl; ?> when the user clicks on the <a> because i never want it to be displayed to the user.
Is there a solution to target a specific anchor to do this? If not then every link on the page is fine I just want to do this in a way that the real URL is not displayed for privacy reasons.
Here is solution.
<?php $newUrl = "http://therealurl.com/folder/private.php"; ?>
<script>
$( document ).ready(function(){
$( '#link' ).click(function(){
var newLink = <?php echo json_encode($newUrl);?>;
$(this).attr("href", newLink);
});
});
</script>
<a id="link" href="http://domain.com">Link</a>
Don't forget to include jquery library.
If you have full access to the original link, you could use it as a redirect by setting a header in the destination script.
header("Location: yoururl.php);
This is the only quick easy solution that I can think of, as this one will not show the actual destination in the source code.
If you want to hide the link from the source code then any javascript methods won't work, to my knowledge.
You could also use ?page=get stuff to change where it sends the user, in order to make this script usable in multiple ways, just don't forget to manage your GET data safely.

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

PHP run in iframe

I created iframe where source is my PHP file, the file should display data from GET method the code is:
<?php
if (isset($_GET["phpMap"])){
var $response = $_GET["phpMap"];
echo $response;
}
?>
but when i run button that send data to that iframe nothing happens.
Plz correct your code as follow,
remove "var"
<iframe src="iframe.php?phpMap='google'" ></iframe>
in iframe.php put code
if (isset($_GET["phpMap"])){$response = $_GET["phpMap"]; echo $response; }
If data is sent using POST then you should update your code and change $_GET[] to $_POST[]
You also dont need to declare your variable like that outside of a Class.
$response = $_POST['phpmap'];
You can also troubleshoot by adding print_r['$_POST'] or print_r['$_GET'] at the top of the script to see what variables if any are coming over. Firebug is also good for capturing POST/GET transmissions and allowing you to see the values being passed back and forth.
You have to change the $_GET into $_POST then it should work with a button click.
If you need to send data from outside of the iframe into the iframe you need to make sure that you write query strings in theurl of the iframe source:
<iframe src="yourfile.php?var=someting"></iframe>
Like that you can recieve the valeue of $var in the script with
<?php $variable = $_GET['var'] ?>

Categories