0

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?

2
  • So basically what you're tring to do is transfer variables/values from one file to another? Is there any particular reason you want to do it this way? Commented May 30, 2012 at 13:44
  • possible duplicate of PHP - Pass POST variables with header()? Commented May 30, 2012 at 13:49

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I try to make login to web site. there are 3 files. html with from that pass data via post to php file. that php file need to check the log in according to the database. if the log in success we pass to another php file that show result that match to this user. I do not want the values ​​appear in the URL.For safety reasons.
5

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.

5 Comments

This makes the server send a request, not the client. So it's probably not what he wants.
How do you make the browser do a post to the new destination using cURL?
Re-reading the question, curl might not be the best solution for this.
I try to make login to web site. there are 3 files.
Ortal, this would work for checking if users are logged in. After you've authenticated the user, simply set their username in the $_SESSION array and check if that variable is present on every other page that is "inside" the logged in section of your site.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.