2011-07-27 82 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

相關問題