views:

282

answers:

4

How do I post a variable in PHP without using a form, and accessing the variable from the next page.

What I am really trying to do though, is post a variable like this:

$username = "Sam";
$_POST[$username];

and then I am trying to access it from a page that I meta refresh to.

Any ideas?

A: 

I don't think it's possible. Try using $_GET instead, or session variables.

Edit: Actually, apparently you can use CURL.

Or this method, which looks like it just write to a file and reads the vars again. I think you're better off using session variables though.

Mark
+9  A: 

What are you trying to achieve? I think you might want Sessions:

// page1.php
session_start();
$_SESSION['username'] = 'Sam';
header('Location: page2.php');

// page2.php
session_start();
print 'Hello, ' . $_SESSION['username'] . '!';

However, if you are sure that is what you want, check out cURL, which will let you POST to a page, but I am not sure it'll let you exactly navigate to it, so to speak. You can get its output, though:

// page1.php
$curl = curl_init('http://www.mysite.com/page2.php');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('username' => 'Sam'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result; // prints "Hello, Sam!"

// page2.php
print 'Hello, ' . $_POST['username'] . '!';
Paolo Bergantino
+1 - I should probably read the whole question lol... SESSIONs are obviously what he wants
John Rasch
Pretty sure it is, but I figured I'd be thorough and post how to achieve it using POST.
Paolo Bergantino
+3  A: 

Look at my answer on the question Passing Information Between PHP Pages, I think it might be of interest to you. It talks about the differences between the few existing methods of communicating between PHP pages.

More specifically, here's a summary in relation to your problem:

  1. With a $_GET (information passed directly in the URL) you might be able to pass your variables, depending on what they are and what you are trying to achieve.

  2. With a $_POST (information posted in the HTTP header to a URL) you won't be able to redirect to a page, because this is done by the browser (client-side) and not by the server (where you are running the PHP script). You could use a hidden form and some javascript to do the post (and the redirection at the same time) though. This might work, depending on your target audience as javascript is not foolproof; it might be disabled on the browser. You can also use CURL as some have said, although this will emulate your server as being the browser; the data will be posted on behalf of your user, but he won't get redirected automatically to the page with the posted data. And if the end result of this posting is authenticating a user for example, your user (client side) won't have a session opened for him.

  3. Using a $_SESSION, with information that remains on the server side. It only (automatically) passes a unique identifier to the browser (normally via a cookie) to link a user to the data you are storing for his session on your website. Of course, this would only be useful if you are redirecting the user to your own application, and not another website.

So depending on your specific problem, there are different possible ways of solving it.

lpfavreau
A: 

will target page know if it's a CURL or form based submission?

abhishek jindal