0

Hi I am new to php and want to know some alternate function for the header('location:mysit.php');

I am in a scenario that I am sending the request like this:

header('Location: http://localhost/(some external site).php'&?var='test')

something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.

I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.

pad
  • 41,040
  • 7
  • 92
  • 166
noobie-php
  • 6,817
  • 15
  • 54
  • 101

4 Answers4

3

You are searching for PHP cUrl:

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • yes almost like this can you kindly tell me how to send variables with it like in header ('Location: http://localhost/(some external site).php'&?var='test') i can set some value in var – noobie-php Nov 21 '11 at 11:44
2

Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.

By using cURL you can pass POST and GET variables to any URL. Like so:

$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Now, in $result you have the response from the URL passed to curl_init().

If you need to post data, the code needs a little more:

$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);

Again, the result from your POST reqeust is saved to $result.

Repox
  • 15,015
  • 8
  • 54
  • 79
0

You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )

matthiasmullie
  • 2,063
  • 15
  • 17