Jump to content

Use cURL to send information to another page


thurft
 Share

Recommended Posts

Hello guys, I am trying to make a validation for a form and if everything goes right it should go to the 2nd page where I should pass the login details. I was told that given the way I am doing it , I should use cURL, but i have no idea what i am doing it and why is not working.... Everything works fine in terms of the validation but somehow the 2nd page is not catching the variables passed by the cURL. It redirects correctly to the install2.php but its not passing any variables to it. What i am doing wrong? I dont know if my problem is in the page 1 in the cURL or in the page 2 in the way im trying to catch the curl My code in the first page is as follows

  <?php //If the person pressed summit check for the information submited. If everything is correct move to the next page  if (isset($_POST['submit'])) {  // declare Variables     $username = $_POST['username'];   $password = $_POST['password'];   $server = $_POST['server'];   $message = "The following fields are empty: ";     //Step 1 : Check if username & server are empty and if they are return the correct error if (empty($username) || empty($server) )   {   if (empty($username)) {   $message .= "Username";		   }   if (empty($password)) {	  $message .= " Password";		  }   if (empty($server)) {  $message .= " Server";		  }   }   else { //Step 2: Attempt to connect to the DB with the information provided. if not sucessful return to correct error to the user. @$connection = mysql_connect($server,$username,$password);if (!$connection) { $message = "Please check your details. the script was unable to connect to the db."; 	 }		  else { //Step 3: Since the connection was stablished successfuly with the information provided then send the login details tot he next page// NOTE: For porpuses of this script I am only passing 1 variable... to make my life easier $data = "username=".$username;$data2 = urlencode ($data); 	  $ch = curl_init (); 	  curl_setopt ($ch, CURLOPT_URL, 'http://localhost/install2.php');	  curl_setopt ($ch, CURLOPT_POST, TRUE);	  curl_setopt ($ch, CURLOPT_POSTFIELDS, $data2);	  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);		  $response = curl_exec ($ch); 	   echo $response;  	   header ('location: install2.php');  	   curl_close ($response);	 } 	   } }?>

and the code in the 2nd page (the one that should catch the code is as follows)

<?php $username = $_POST['username'];$password = $_POST['password'];$server = $_POST['server']; echo "Welcome to install 2" . '</br>'; if (!empty($username)){ echo "the user name is {$username} and the password is {$password} and the server is" . '</br>';}  ?>

Edited by thurft
Link to comment
Share on other sites

When you redirect using a location header it does not send post data. When you use cURL you are sending the request from the server, it's a request from one server to another. You're not redirecting the user. It won't send the user's cookies or session information, for example, because their browser is not making the request. You can output the response like you're doing, but that's going to be the response to the server, not the browser. If you have a multi-step process where you need to keep passing the same data from page to page the common ways are to either use hidden form fields with the data you want to propagate, or store the data in the session and look it up on each subsequent page.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...