PHP-使用cURL将信息发送到另一个页面
我正在尝试对表单进行验证,如果一切正常,它应该转到第2页,在那里它应该从$u帖子中获取信息 有人告诉我,以我现在的方式,$\u POST不起作用,我应该试试cURL。我试着让它工作,但不知怎的它不工作。它会正确重定向到PHP-使用cURL将信息发送到另一个页面,php,forms,curl,Php,Forms,Curl,我正在尝试对表单进行验证,如果一切正常,它应该转到第2页,在那里它应该从$u帖子中获取信息 有人告诉我,以我现在的方式,$\u POST不起作用,我应该试试cURL。我试着让它工作,但不知怎的它不工作。它会正确重定向到install2.php,但不会向其传递任何变量。我做错了什么 我的代码如下: <?php //If the person pressed summit check for the information submited. If everything is correct
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');
}
}
}
?>
这不是传递帖子字段的方式:
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');
这次是一个没有参数的调用,它解释了您的问题。您的意思是“您没有读取响应,只是重定向到用户?”我做了您建议的更改,但它没有传递这些更改,或者没有通过另一个文件中的$\u POST获取这些更改。我通过另一个文件上的post捕捉变量,对吗??我想将变量发送到install2.php。。。这里有点混乱:Safter
$response=curl\u exec($ch)代码>只需执行:echo$response代码>我添加了它。。。但是当我去安装时,2.php没有捕捉到任何东西。。。我应该用“install2.php”中的_POST捕捉变量还是使用其他东西?curl返回一个内容,如果您想要的是一个简单的重定向,您不需要使用它,只需调用header('location:install2.php?username=$username&password=$password')代码>