2011-12-31 45 views
2

好的,所以我正在嘗試爲iPhone和Android開發移動網站應用程序。目前我的網站使用cURL將用戶登錄到其他網站。我有一個PHP腳本,根據用戶的用戶名創建一個cookie。 cURL然後將信息放入該cookie中。該cookie存儲在我的網站的主機上。更好的方法來存儲用戶憑證

基本上我創建的這個移動網站是假設允許用戶登錄到我爲此開發的論壇(網站所有者不允許我在他們的網站上創建移動版本,因此需要在我的網站上執行此操作)。然後,一旦他們登錄,他們可以閱讀帖子並回復。當它讀取一個線程需要加載cookie,以及他們試圖發表帖子的時候。

如何獲取cookie以保存到用戶手機而不是我的服務器?我問的原因是,我喜歡它,所以我的主機沒有得到數十個帶有用戶憑據的文本文件(我不想看到,因此我不是釣魚網站)。

我想讓用戶登錄,cookie會保存到手機中。他們想要閱讀一篇帖子,手機會提取該cookie。他們想發帖,手機拿起cookie。

我看着PHP的setcookie()函數,不知道這是否是我需要的。

提供的任何幫助將不勝感激。

回答

0

當您在服務器端設置一個cookie,通過稱爲HTTP Headers的cookie發送給客戶端(您的手機)。有一個名爲「Set-Cookie」的HTTP頭和一個cookie的值。當瀏覽器將來向服務器發送請求時,它將會在HTTP標頭中給出該值,稱爲「Cookie」

因此,如果您想設置一個cookie並使用該cookie,來自您的請求的Cookie,將其存儲在某個安全的地方,並在將來的請求中將其返回。

http://en.wikipedia.org/wiki/HTTP_cookie

下面是一個簡單的驗證方法,其採用的URL,用戶名和密碼,並返回Cookie值。

static public String authenticate(String service_url, String username, String password) throws IOException 
{ 
    if (username == null || password == null) 
     throw new IOException(); 

    String charset = "UTF-8";  
    URL url = new URL(service_url); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+charset); 
    connection.setDoOutput(true); 
    connection.setRequestMethod("POST"); 
    connection.setReadTimeout(5000); // 2 second timeout. 

    String query = String.format("Email=%s&Password=%s", 
      URLEncoder.encode(username, charset), 
      URLEncoder.encode(password, charset)); 

    OutputStream output = null; 
    try { 
     output = connection.getOutputStream(); 
     output.write(query.getBytes(charset)); 
    } finally { 
     if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} 
    } 

    connection.getInputStream(); 

    List<String> cookies = connection.getHeaderFields().get("Set-Cookie"); 
    if (cookies == null) 
     throw new IOException(); 

    for (String cookie : cookies) 
    { 
     if (cookie.startsWith("authcookie")) 
      return cookie; // this is the only correct path out. 
    } 
    throw new IOException(); 
} 

示例HTTPGET,請注意HTTP標頭以將cookie值添加回請求。

public static InputStream getDataFromHTTP(String url, String authenticationCookie, String  mimetype) throws ClientProtocolException, IOException 
{  
    DefaultHttpClient client = getHttpClient();  

    if (client == null) 
     throw new IOException("Cant getHttpClient()"); 

    if (url == null) 
     throw new IOException("URL is null"); 

    HttpGet httpget = new HttpGet(url); 
    httpget.addHeader("Accept", mimetype); 
    httpget.addHeader("Cookie", authenticationCookie);  
    httpget.addHeader("Accept-Encoding", "gzip"); 
    HttpResponse response = client.execute(httpget); 

    InputStream instream = response.getEntity().getContent(); 
    Header contentEncoding = response.getFirstHeader("Content-Encoding"); 

    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { 
     instream = new GZIPInputStream(instream); 
    } 

    return instream; 
}