Ortal
Ortal

Reputation: 1

how to send data via header function like post

I try to pass data from php file to another php file with header function. like that:

header("location:login.php?userName=$username"); 

(that line appear in the first php file.) the problem is that the data appear in the url of the second php file. like GET. I try looking for some example but without success. i looking for how to make it like POST?

Upvotes: 0

Views: 4782

Answers (3)

nageeb
nageeb

Reputation: 2042

Originally suggesting to use CURL, I now believe that it might not be the best solution. If you're simply looking to preserve or pass data between scripts, you might want to look into Sessions, which allow you to set and preserve data between scripts.

eg:

Script 1

session_start();

$_SESSION['userName'] = $username;

Script 2

session_start();

if (isset($_SESSION['userName']))
    $username = $_SESSION['userName'];

echo $username;

This will successfully pass the value of $username between two scripts. Simply put any variables into the $_SESSION array and they will be available throughout your scripts.

  • Be sure to include session_start(); at the top of every script
  • Look at also including session_set_cookie_params() to be sure your sessions are available through your domain and/or don't conflict with other site's sessions.

Upvotes: 5

cweiske
cweiske

Reputation: 31078

You could use PEAR's excellent HTTP_Request2 package to make POST requests.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318488

You can't. If you redirect you'll always have a GET request.

A possible solution (if the target script is on the same domain) would be using PHP sessions to store the data on the server.

Upvotes: 7

Related Questions