tags:

views:

215

answers:

8

I had a hyperlink like this

http://www.mywebsite.com/sendmessage.php?uid=siva&pwd=mani&phone=newairtel&msg=dg

Normally in HTML we put href to that link and the link will open after the user click that link.

But I want that link to be executed automatically without user interaction using PHP. How can i get it.

Plz help. Any Help will be highly appreciated

I DONT WANT THAT PAGE SHOULD BE REDIRECTED. JUST I NEED THE LINK TO EXECUTE

+7  A: 

PHP operates only on the server. Hyperlinks are clicked on the client. For this reason, PHP cannot directly invoke the link. You can have PHP write some Javascript that will click the link once the page has loaded,

<?php if ($shouldClickLink) { ?>
  <script type="text/javascript">
    $(function(){
      $("a.someLink").trigger('click');
    });
  </script>
<?php } ?>

/* or, alternatively */

<?php if ($shouldClickLink) { ?>
  <script type="text/javascript">
    window.location = 'http://www.google.com';
  </script>
<?php } ?>

or you can use the header() function in PHP to automatically go to a new URL.

<?php if ($shouldClickLink) { header('Location: http://www.google.com'); }  ?>

Those are your options.

Jonathan Sampson
A: 

The only way you can do it is to set the required header before any output has been sent to the page (you do this with the header() function). Otherwise you will need to use a client side language like javascript to do it, but it is less reliable as the user may have javascript disabled, so if you use a javascript solution you also need to display the link in html on the page and tell the user to do it

Before any output has been sent (You can't set headers after output has been sent to the client), you can do this with PHP

header("Location: http://www.example.com/something.php?a=1");
exit();//don't send any output after this

However, if you wanted to do it with javascript, you just need to insert this into the document (within <script> tags)

window.location = "http://www.example.com/something.php?a=1";
Yacoby
+2  A: 

One option would be to use file_get_contents. It will load any url you give it and return the content as a string. The link will execute and you can just ignore the returned content if you don't need it:

$content = file_get_contents('http://www.mywebsite.com/sendmessage.php?uid=siva&amp;pwd=mani&amp;phone=newairtel&amp;msg=dg');
Chris Pebble
+2  A: 

I think what you're wanting is Header location referral.

http://php.net/manual/en/function.header.php

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>
Crowe T. Robot
+2  A: 

if you mean an actual redirect to that page so the user is taken to that URL you could just do:

 <?php 
 header("Location: http://www.mywebsite.com/sendmessage.php?uid=siva&amp;pwd=mani&amp;phone=newairtel&amp;msg=dg");
 exit();
 ?>

... or to grab the contents of that URL you could do:

 <?php
 $contents = file_get_contents("http://www.mywebsite.com/sendmessage.php?uid=siva&amp;pwd=mani&amp;phone=newairtel&amp;msg=dg");
 ?>

EDIT file_get_contents will cause the remote script to execute so you can just ignore what gets returned if you want but this should achieve what you are looking to do

seengee
I DONT WANT THAT PAGE SHOULD BE REDIRECTED. JUST I NEED THE LINK TO EXECUTE
Rajasekar
woah, since i'm trying to help i'll assume you're not shouting
seengee
You can still execute your PHP before redirecting. Or, if you need the second page to execute asynchronously, you can use javascript to fire off an ajax request.
keithjgrant
+2  A: 

Try a JavaScript function that opens a new window to your desired URL on

<body onload="javascript:openURL('http://...')"

antik
+1  A: 

It appears that you want to send mail every time a user goes to a particular page? If this is the case, then what you need is to include the mail processing file in the PHP file that creates your page. Like so:

<?php
include "sendmessage.php"; //this sends the mail 

//rest of page
?>
dnagirl
A: 

What does it mean for a link to "execute"? Normally this means that a user's browser will navigate to the target of the link. How is this different from redirection?

If you want the script at sendmessage.php to be called while keeping the user on the same page, consider loading that as an invisible iframe or frame...

<iframe src="http://www.mywebsite.com/sendmessage.php?uid=siva&amp;pwd=mani&amp;phone=newairtel&amp;msg=dg" width="1" height="1">
Yuliy