【问题标题】:Sending data from one php file to other php file [closed]将数据从一个php文件发送到另一个php文件[关闭]
【发布时间】:2019-04-17 00:29:43
【问题描述】:

我想将数据从一个 php 文件发送到另一个 php 文件以验证用户身份。它们都不能是 html 文件。而且我无法将它作为标题发送,因为它出现在 url 中。请给我一些建议。

【问题讨论】:

  • 我认为您需要稍微扩展您的问题。这些文件在同一台服务器上吗?文件里有什么?
  • @Matt 文件在同一服务器,同一目录中。一个文件包含身份验证信息(用户名、密码等),另一个文件将验证信息。而且我无法嵌入这两个文件
  • 认证信息(用户名、密码)是否存储为数组?
  • 如果用户不能是html文件,他们不能使用HTTP协议,例如$_POST 还是 $_SESSION?和重定向? :)

标签: php


【解决方案1】:

您可以为此使用CURL 发送安全的POST 请求,即使如果接收方脚本位于另一台服务器上。这不会暴露 URL 中的任何内容,但是 Firebug 可能能够看到请求。要解决此问题,只需确保您的 auth key 和正在发送的密码是 hashed

类似的东西(未经测试)

这里是发送者脚本

<?php

///// Sender.php ///////

//Set up some vars
$url = 'http://domain.com/Receiver.php';

$user = 'someusername';
$pw = 'somepassword';
$auth_key = 'YourSecretAuthKey';

$fields = array(
            'auth'=>urlencode($auth_key),
            'user'=>urlencode($user),
            'pw'=>urlencode($pw)
        );

// Init. string
$fields_string = '';
// URL-ify stuff
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);


?>

以及接收脚本:

<?php

////// Receiver.php //////

if(!isset($_POST['authkey']))die('Error: No Auth Key');
if(!isset($_POST['user']))die('Error: No Username!');
if(!isset($_POST['pw']))die('Error: No password!');

$auth_key = $_POST['auth'];
$correct_authkey = 'YourSecretKey';

if($auth_key!=$correct_authkey)die('WRONG AUTH KEY!!!!');

$user = $_POST['user'];
$pw = $_POST['pw'];

//// Here you can process your username and password

?>

POST 请求非常安全,但如果您正在处理关键信息,您始终可以散列身份验证密钥和密码。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    加密或散列身份验证数据并作为POST 正文的一部分或在 URL 中发送。在接收端,查看$_POST$_GET

    【讨论】:

    • $_GET 违背了 OP 的首选解决方案。
    【解决方案3】:

    因此,您需要将身份验证信息包含到另一个文件中:

    <?php
    require_once('authentication_infomation.php');
    doTheAuthentication($authentication_information);
    ?>
    

    见:

    http://se.php.net/manual/en/function.require-once.php

    更多信息。

    【讨论】:

    • And I can't embed the two files - OP 对他的 OP 的评论。 :)
    • @Jeff 我可能误解了该评论。我认为 OP 由于不知道如何嵌入文件而无法嵌入文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2016-03-25
    • 2016-04-13
    • 2023-03-08
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多