2011-04-05 49 views
0

更新: 我发现了一个可能导致问题的IIS错误。看到这个职位IIS and nphJoomla - 通过脚本登录来宾用户

_________________Original Question_______________________________________________________________________________

我有访问我的网站的私人较少,但仍私有,部分游客的Joomla用户。我想一些用户,通过包含哈希一个特殊的链接访问,能够在有限的访问Guest帐户自动登录。

要做到这一点,我在下面的帖子布伦特修士Logging in using cURL。该过程的

地方工作。

  • 我能够做出cURL请求,刮取令牌值。
  • 运行脚本后,我可以看看Joomla的会话表,看看我的访客用户的条目。
  • 当我打印之前我设置cookie的值 - 它在会话表中的cookie匹配。

然而,当我试图进入该网站的访客用户应该能够访问的区域,我重定向到登录界面。当我在浏览器中检查cookie时,它与脚本中打印的Cookie不同。然后,如果我再次检查会话表,我发现新的cookie与更新的匿名会话相关联。

为什么饼干在setCookie方法()设置不坚持?

这里是我的代码:

$uname = "DocGuest"; 
     $upswd = "pass"; 

     //This is the URL of the normal login form on the website 
     $url = "http://localhost/index.php?option=com_content&view=article&id=115&Itemid=283"; 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
     curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('./cookie.txt')); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('./cookie.txt')); 
     curl_setopt($ch, CURLOPT_HEADER, TRUE); 
     $ret = curl_exec($ch); 
     if (!preg_match('/name="([a-zA-z0-9]{32})"/', $ret, $spoof)) { 
      preg_match("/name='([a-zA-z0-9]{32})'/", $ret, $spoof); 
     } 

     $postfields = array(); 
     $postfields['username'] = $uname; 
     $postfields['passwd'] = $upswd; 
     $postfields['lang'] = 'en'; 

     $postfields['option'] = 'com_user'; 
     $postfields['task'] = 'login'; 
     $postfields[$spoof[1]] = '1'; 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
     $ret = curl_exec($ch); 


     // Get logged in cookie and pass it to the browser 
     preg_match('/^Set-Cookie: (.*?);/m', $ret, $m); 
     $cookie=explode('=',$m[1]); 
     print_r($cookie); //Cookie matches session table here? 


     //header("location: http://localhost/index.php?option=com_k2&view=itemlist&layout=category&task=category&id=1&Itemid=158"); 
     setcookie($cookie[0], $cookie[1], 3600*24, '/'); 
+0

@Brent弗莱尔 - 我铸造蝙蝠信号给你,布伦特弗莱尔:) – tpow 2011-04-05 14:51:19

回答

1

哇,有趣的问题,我会假设你正在使用XAMPP由于域。我看到可能导致问题的第一件事是在传输cookie之前的重定向。

//header("location: http://localhost/index.php?option=com_k2&view=itemlist&layout=category&task=category&id=1&Itemid=158"); 
setcookie($cookie[0], $cookie[1], 3600*24, '/'); 

应该是:

setcookie($cookie[0], $cookie[1], 3600*24, '/'); 
header("location: http://localhost/index.php?option=com_k2&view=itemlist&layout=category&task=category&id=1&Itemid=158"); 

其他的事情,我会做的路径未设置到cookie。我知道在过去,我遇到了XAMPP路径问题,并不像我认为的那样工作。尝试使用:

setcookie($cookie[0], $cookie[1], 3600*24); 
+0

感谢您的时间 - 我更新了我的问题。我实际上在IIS7上匹配生产环境(我知道,我宁愿在LAMP上)。至于Header/Cookie订单。我也这么认为,但事实证明,网络服务器不应该关心订单。另外,在PHP文档的setCookie页面 - 大约10条评论,有一篇文章提到重定向应该是第一位的。两者都试过 – tpow 2011-04-06 11:34:10

相关问题