0

I am trying to make a validation for a form and if everything goes right it should go to the 2nd page, where it should take the information from the $_POST.

I was told that given the way I am doing it, $_POST would not work and I should try cURL. I tried to make it work, but somehow it does not work. It redirects correctly to the install2.php but its not passing any variables to it. What i am doing wrong?

My code 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
            $ch = curl_init (); 
            curl_setopt ($ch, CURLOPT_URL, 'http://localhost/install2.php');
            curl_setopt ($ch, CURLOPT_POST, TRUE);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $username);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);

            $response = curl_exec ($ch);

            curl_close ($responde);
            header ('location: install2.php');
        }
    }
}
?>
0

2 Answers 2

1

That's not the way to pass post-fields:

curl_setopt ($ch, CURLOPT_POSTFIELDS, $username);

you should create a string:

$data = "username=$username&password=$password";

and pass it:

curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);

Second, better encode the parameters you're passing using urlencode().

And last, it DOES pass the parameters when you call:

$response = curl_exec ($ch);

but, then you don't read the response and just redirect the user to:

header ('location: install2.php');

this time it's a call with no parameters which explains your problem.

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

4 Comments

What you mean "you dont read the response and just redirect to the user?" I made the changes your suggested but its not passing them or is not getting them by a $_POST in the other file. I catch the variables via post on the other file right?? I want to send the variable to the install2.php... kinda confused here :S
after $response = curl_exec ($ch); just do: echo $response;
I added it... but when i go to the install2.php is not catching anything... should i be catching the variables with _POST in "install2.php" or using something else?
curl returns a content, if what you want is a simple redirect you don't need to use it, just call header ('location: install2.php?username=$username&password=$password');
0

I remarked that your variable name is not the same.

$reponse and $responde 
$response = curl_exec ($ch);

curl_close ($responde);
header ('location: install2.php');

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.