2011-07-27 83 views
1

我想从一个php文件发送数据到其他php文件来验证用户。它们都不能是html文件。我不能将它作为标题发送,因为它出现在网址中。 Plz给我一些建议。从一个php文件发送数据到其他php文件

+1

我认为你需要扩大你的问题一点。这些文件在同一台服务器上吗?这些文件中包含什么内容? –

+0

@Matt同一个服务器,同一目录中的文件。一个文件包含认证信息(用户名,密码等),另一个文件将验证信息。我无法嵌入这两个文件 – Nitish

+0

是否将认证信息(用户名,密码)存储为数组? –

回答

4

您可以使用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要求是相当安全的,但如果你正在处理至关重要的信息,您总是可以散列授权密钥和密码。希望这可以帮助。

0

对认证数据进行加密或散列处理,并作为POST正文的一部分或在URL中发送。在接收端,请看$_POST$_GET

+1

$ _GET违背OP的首选解决方案。 – Jeff

0

您需要包括的认证信息到正是如此的其他文件:

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

参见:

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

欲了解更多信息。

+0

'我不能嵌入这两个文件' - OP对他的OP的评论。 :) – Jeff

+0

@杰夫我可能误解了这个评论。我认为OP由于不知道如何去做而无法嵌入文件。 –

+0

也许我也误解了,谁知道? :P – Jeff

相关问题