PHP - 使用cURL将信息发送到另一个页面 [英] PHP - Use cURL to send information to another page

查看:139
本文介绍了PHP - 使用cURL将信息发送到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对表单进行验证,如果一切正常,应该去第二页,它应该从$ _POST获取信息。

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.

我被告知,给定我的方式,$ _POST不会工作,我应该尝试cURL。我试图使它的工作,但不知何故,它不工作。它正确地重定向到 install2.php ,但它没有传递任何变量到它。我做错了什么?

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?

我的代码如下:

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


推荐答案

这不是传递后场的方式: br>

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";

并传递:

curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);

第二,更好的编码你传递的参数 urlencode / code>。

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.

这篇关于PHP - 使用cURL将信息发送到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆