2012-01-05 44 views
-2

我試着找到我的問題的解決方案的谷歌搜索。用`curl_setopt`在onether文件中設置cookie等

我需要在一個不同的文件中設置一個cookie,我試着用curl_setopt,但它不起作用。

任何方式的想法是用PHP發送到設置cookie的另一個網頁的價值,這樣的事情:

file1.php 

    <? //start php 
    //at the begining of the file i have 
session_start(); 
header('Content-Type: text/html; charset=utf-8'); 
//if i set a cookie now it give me an error cause i can not change the header 
//but because i need to set a cookie now without leaving this file 
//i tryed to set it in file2.php this way: 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $SITE_HOME_DIR ."login.php"); 
// Do a POST 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '[email protected]'); 
curl_close($ch); 
    ///end php 
    ?> 

    file2.php 
    <? 
//just set cookie 
setcookie("TestCookie", $_POST['email'], time()+3600) 
?> 

但這doesn'work ....

任何想法?謝謝。

+1

你需要說明你的「不同的文件」或「其他頁面」的概念。 Cookie通常設置爲['setcookie'](http://php.net/setcookie),而不是['curl_setopt'](http://php.net/curl_setopt)。 – mario 2012-01-05 20:35:05

+0

好的我編輯了我的問題... – 2012-01-05 21:12:31

+0

仍然不清楚你的實際目標是什麼。在第一個文件中使用'setcookie'有什麼問題?解釋你如何期望這種行爲在哪個環境下進行。 (編輯後的代碼沒有任何說明。) – mario 2012-01-05 21:14:40

回答

0

您只能爲setcookie()設置下一個腳本的Cookie。沒有其他選擇。 - cURL向其他頁面發送不同的請求,但不能在客戶端(瀏覽器)設置任何cookie。

您的錯誤在Headers already sent (reference answer)中解釋。如果您不能/不會重寫您的頁面,您可能會或可能不會運用上述ob_start()解決方法。

+0

您是否閱讀過鏈接? – mario 2012-01-05 23:22:27

+0

你是對的......我很抱歉我沒有......但我現在讀了它。謝謝你。但有趣的是,我嘗試setcookie後會話開始和標題,它正在工作!我希望繼續工作。 – 2012-01-06 00:11:30