0

我正在尝试对表单进行验证,如果一切顺利,它应该转到第二页,它应该从 $_POST 获取信息。

有人告诉我,鉴于我这样做的方式,$_POST 不起作用,我应该尝试 cURL。我试图让它工作,但不知何故它不起作用。它正确地重定向到,install2.php但它没有向它传递任何变量。我做错了什么?

我的代码如下:

<?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');
        }
    }
}
?>
4

2 回答 2

1

这不是传递后字段的方式:

curl_setopt ($ch, CURLOPT_POSTFIELDS, $username);

你应该创建一个字符串:

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

并通过它:

curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);

其次,更好地编码您使用传递的参数urlencode()

最后,它确实在您调用时传递参数:

$response = curl_exec ($ch);

但是,您不会阅读响应,而只是将用户重定向到:

header ('location: install2.php');

这次是一个没有参数的调用来解释你的问题。

于 2012-07-18T23:11:39.933 回答
0

我注意到你的变量名不一样。

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

curl_close ($responde);
header ('location: install2.php');
于 2022-02-06T15:44:01.670 回答