新人求教PHPThink 关于session和cookie的问题

公西雒 2014-03-27 03:33:56
WPF(C#)开发的客户端,想要实现打开浏览器并登录网站的功能。参考了这个http://blog.csdn.net/htsnoopy/article/details/7094224
我通过c#代码生成了一个cookie,但是打开浏览器跳转网页的时候发现session里面的值是空的。
仔细研究了一下才发现,PHPThink框架开发的网站每次都会生成固定的sessionid,而我的代码每次生成的sessionid都是随机的,对不上号所以也就登录不了,怎么办呢?
...全文
388 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏东坡发火 2014-03-28
  • 打赏
  • 举报
回复
没改的话php中的session是概率刷新的,默认只有千分之一,现在phpthink那边每次都会随机产生,估计配置文件里面设置了每次重新打开网站session百分之百刷新 你打开php.ini 看一下session.gc_probability和session.gc_divisor的值分别是多少,这两个值影响session的刷新概率 现在你估计只要把里面的session.gc_probability设为0就能保证session不刷新
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 21 楼 u014408680 的回复:
c#不懂,但你每次连接服务器服务器都会重新建立一个session(服务器端配置文件里面设置的?),但是你的客户端这边好像并没有再次接受这个sessionid,还是使用以前的sessionid,你在程序中写上当退出页面的时候删除cookie行不行呢
之前的php配置文件我从来没动过,后来修改了一下,不好使然后又改了回去。。。删除cookie肯定也是不行的撒,那个固定的cookie根本就没有用户的信息。
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 20 楼 xuzuning 的回复:
如果有 session 那么他在 cookie 数据中表现为 PHPSESSID:................. 你打印出的数据中没有这样的内容
谢谢大神,仔细思考了一下,我那些数据是通过chromeF12跟踪到的FormData,具体什么是token,新手不懂,但是我感觉我获取的cookie没有这个数据,发送的cookie也没有,PHPSESSID在我的程序中就是cookie.Value的值,这个是我跟踪调试时发现的,而且在我获取cookie的时候,服务器存session的文件夹里面已经有对应id的session了,里面的内容也存了admin,只是没有密码信息,可能是安全考虑。 下面我把session的内容发出来,您给看看,该怎么弄。 先发生成的固定session,sess_60k402oc795mpvihq9jodpnml7
GuestName|s:5:"admin";UserId|N;__hash__|a:2:{s:32:"29b296010dbae92683f9cfada6743386";s:32:"f6f026587c241f4f2f6f94ed4e957ccb";s:32:"d150a69b13ade312006133d91f4fc9b1";s:32:"ef751c9ebc0066c0a98731b2b819ac98";}
再发随机生成的session,sess_ln3gn3acd5a6arme32h3viq3e5
GuestName|s:5:"admin";UserId|N;
如果不打开客户端直接网站登录,那么只会生成固定的那个session;如果打开客户端登录,就会先生成一个随机的,当通过客户端打开浏览器登录时,又会生成一个固定的,此时固定的没有GuestName信息。 其中GuestName是用户名的信息,网站的一切操作权限都是关联的这个GuestName,UserId始终是N,不知道是什么意思,__hash__我感觉是操作生成的记录信息,因为每一个页面跳转操作都会刷新__hash__内容。 我的问题出在哪里呢?
苏东坡发火 2014-03-28
  • 打赏
  • 举报
回复
c#不懂,但你每次连接服务器服务器都会重新建立一个session(服务器端配置文件里面设置的?),但是你的客户端这边好像并没有再次接受这个sessionid,还是使用以前的sessionid,你在程序中写上当退出页面的时候删除cookie行不行呢
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
如果有 session 那么他在 cookie 数据中表现为 PHPSESSID:................. 你打印出的数据中没有这样的内容
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 18 楼 xuzuning 的回复:
应该是 你贴出的数据中并没有 sessionid 你怎么总是取检查 session 呢?方向错了
o8ru11js96qn9rneqokpc6alo2这个是sessionid 但是是随机的 网站自己生成的sessionid是固定的。 哪个方向错了啊!大神多指点指点,不懂呢
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
应该是 你贴出的数据中并没有 sessionid 你怎么总是取检查 session 呢?方向错了
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 16 楼 xuzuning 的回复:
说错了,要同时打印 cookie 变量名和值 tp 的表单有可能使用了 token,也要处理的 模拟登录的流程是: 1、访问登陆页,获取表单元素名和值(已知的话可跳过) 2、获取 cookie、获取可能存在的 token 3、获取可能存在的验证码图片并解析。如果有验证码,还需重新获取 cookie 4、发送应提交的数据集合和 cookie 到表单目标页
admin:admin
password:424
__hash__:9dcee4d5bcfb44270f8cbfaebbc2d786_a57a03650a2256a207ed4f6ccaad6bf0
没有验证码,这个__hash__是您指的token吗?
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
说错了,要同时打印 cookie 变量名和值 tp 的表单有可能使用了 token,也要处理的 模拟登录的流程是: 1、访问登陆页,获取表单元素名和值(已知的话可跳过) 2、获取 cookie、获取可能存在的 token 3、获取可能存在的验证码图片并解析。如果有验证码,还需重新获取 cookie 4、发送应提交的数据集合和 cookie 到表单目标页
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 13 楼 xuzuning 的回复:
打印出你获取到的 cookie 的值
GuestName|s:5:"admin";UserId|N;这个?
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 13 楼 xuzuning 的回复:
打印出你获取到的 cookie 的值
o8ru11js96qn9rneqokpc6alo2是这个吗?
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
打印出你获取到的 cookie 的值
公西雒 2014-03-28
  • 打赏
  • 举报
回复
登陆页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
		
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		
		<title>后台登陆</title>
		
		<!--                       CSS                       -->
	  
		<!-- Reset Stylesheet -->
		<link rel="stylesheet" href="{$Think.config.public_path}/css/reset.css" type="text/css" media="screen" />
	  
		<!-- Main Stylesheet -->
		<link rel="stylesheet" href="{$Think.config.public_path}/css/style.css" type="text/css" media="screen" />
		
		<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
		<link rel="stylesheet" href="{$Think.config.public_path}/css/invalid.css" type="text/css" media="screen" />	
		
		<!-- Colour Schemes
	  
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
	 
		-->
		
		<!-- Internet Explorer Fixes Stylesheet -->
		
		<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
		
		<!--                       Javascripts                       -->
	  
		<!-- jQuery -->
		<script type="text/javascript" src="{$Think.config.public_path}/scripts/jquery-1.3.2.min.js"></script>
		
		<!-- jQuery Configuration -->
		<script type="text/javascript" src="{$Think.config.public_path}/scripts/simpla.jquery.configuration.js"></script>
		
		<!-- Facebox jQuery Plugin -->
		<script type="text/javascript" src="{$Think.config.public_path}/scripts/facebox.js"></script>
		
		<!-- jQuery WYSIWYG Plugin -->
		<script type="text/javascript" src="{$Think.config.public_path}/scripts/jquery.wysiwyg.js"></script>
		
		<!-- Internet Explorer .png-fix -->
		
		<!--[if IE 6]>
			<script type="text/javascript" src="resources/scripts/DD_belatedPNG_0.0.7a.js"></script>
			<script type="text/javascript">
				DD_belatedPNG.fix('.png_bg, img, li');
			</script>
		<![endif]-->
		
	</head>
  
	<body id="login">
		
		<div id="login-wrapper" class="png_bg">
			<div id="login-top">
			
				<h1>系统</h1>
				<!-- Logo (221px width) -->
				<img id="logo" src="{$Think.config.public_path}/images/logo.png"  />
			</div> <!-- End #logn-top -->
			
			<div id="login-content">
				
				<form  action="__URL__/login_into" method="post">
				
				<!--	<div class="notification information png_bg">
						<div>
							Just click "Sign In". No password needed.
						</div>
					</div>-->
					
					<p>
						<label>用户名</label>
						<input name="admin" class="text-input" type="text" />
					</p>
					<div class="clear"></div>
					<p>
						<label>密  码</label>
						<input name="password" class="text-input" type="password" />
					</p>

					<div class="clear"></div>
					<p>
						<input class="button" type="submit" value="登录" />
					</p>
					
				</form>
			</div> <!-- End #login-content -->
			
		</div> <!-- End #login-wrapper -->
		
  </body>
  </html>
公西雒 2014-03-28
  • 打赏
  • 举报
回复
httphelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace WpfUI
{
    public class HTMLHelper
    {
       /// <summary>
        /// 获取CooKie
       /// </summary>
       /// <param name="loginUrl"></param>
       /// <param name="postdata"></param>
       /// <param name="header"></param>
       /// <returns></returns>
       public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
       {
           HttpWebRequest request = null;
           HttpWebResponse response = null;
           try
           {
               CookieContainer cc = new CookieContainer();
               request = (HttpWebRequest)WebRequest.Create(loginUrl);
               request.Method = header.method;
               request.ContentType = header.contentType;
               byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
               request.ContentLength = postdatabyte.Length;
               request.AllowAutoRedirect = false;
               request.CookieContainer = cc;
               request.KeepAlive = true;

               //提交请求
               Stream stream;
               stream = request.GetRequestStream();
               stream.Write(postdatabyte, 0, postdatabyte.Length);
               stream.Close();

               //接收响应
               response = (HttpWebResponse)request.GetResponse();
               response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

               CookieCollection cook = response.Cookies;
               //Cookie字符串格式
               string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

               return cc;
           }
           catch (Exception ex)
           {
               
               throw ex;
           }
       }

       /// <summary>
       /// 获取CookieCollection
       /// </summary>
       /// <param name="loginUrl"></param>
       /// <param name="postdata"></param>
       /// <param name="header"></param>
       /// <returns></returns>
       public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)
       {
           HttpWebRequest request = null;
           HttpWebResponse response = null;
           try
           {
               CookieContainer cc = new CookieContainer();
               request = (HttpWebRequest)WebRequest.Create(loginUrl);
               request.Method = header.method;
               request.ContentType = header.contentType;
               byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
               request.ContentLength = postdatabyte.Length;
               request.AllowAutoRedirect = false;
               request.CookieContainer = cc;
               request.KeepAlive = true;

               //提交请求
               Stream stream;
               stream = request.GetRequestStream();
               stream.Write(postdatabyte, 0, postdatabyte.Length);
               stream.Close();

               //接收响应
               response = (HttpWebResponse)request.GetResponse();
               response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

               CookieCollection cook = response.Cookies;
               //Cookie字符串格式
               string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

               return cook;
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }

       /// <summary>
       /// 获取html
       /// </summary>
       /// <param name="getUrl"></param>
       /// <param name="cookieContainer"></param>
       /// <param name="header"></param>
       /// <returns></returns>
       public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
       {
           Thread.Sleep(1000);
           HttpWebRequest httpWebRequest = null;
           HttpWebResponse httpWebResponse = null;
           try
           {
               httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
               httpWebRequest.CookieContainer = cookieContainer;
               httpWebRequest.ContentType = header.contentType;
               httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
               httpWebRequest.Referer = getUrl;
               httpWebRequest.Accept = header.accept;
               httpWebRequest.UserAgent = header.userAgent;
               httpWebRequest.Method = "GET";
               httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
               Stream responseStream = httpWebResponse.GetResponseStream();
               StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
               string html = streamReader.ReadToEnd();
               streamReader.Close();
               responseStream.Close();
               httpWebRequest.Abort();
               httpWebResponse.Close();
               return html;
           }
           catch (Exception e)
           {
               if (httpWebRequest != null) httpWebRequest.Abort();
               if (httpWebResponse != null) httpWebResponse.Close();
               return string.Empty;
           }
        }
    }

    public class HttpHeader
    {
        public string contentType { get; set; }

        public string accept { get; set; }

        public string userAgent { get; set; }

        public string method { get; set; }

        public int maxTry { get; set; }
    }
}
公西雒 2014-03-28
  • 打赏
  • 举报
回复
C#获取cookie登录部分
            HttpHeader header = new HttpHeader();
            header.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            header.contentType = "application/x-www-form-urlencoded";
            header.method = "POST";
            header.userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E";
            header.maxTry = 200;

            CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://localhost/gyjw/index.php/index/login_into",
                    "admin=admin&password=424", header);

            foreach (Cookie cookie in mycookie) //将cookie设置为浏览的cookie  
            {
                InternetSetCookie(

                    "http://" + cookie.Domain.ToString(),

                    cookie.Name.ToString(),

                    cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");

            }
            //System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/index/Login.html");
            //System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/index/login_into");
            //System.Diagnostics.Process.Start("http://localhost/gyjw/index.php");
            System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/user/AddUser.html");
net_php 2014-03-28
  • 打赏
  • 举报
回复
你还是贴代码吧!
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
那你的 php 部分是怎么写的? 如果“无论怎么弄她都是固定的id”,那你读到的和发回去的不也是那个固定的id吗?
不是哦 什么原因呢?
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
是的,浏览器只懂html 嗯,还懂 js
公西雒 2014-03-28
  • 打赏
  • 举报
回复
引用 30 楼 xuzuning 的回复:
既然用浏览器没有问题,显然浏览器也不会懂 phpthink 的 你的客户端只是模拟了浏览器的行为,自然也不需要懂 phpthink
那phpthink是怎么运行的?浏览器只懂html吗?
xuzuning 2014-03-28
  • 打赏
  • 举报
回复
既然用浏览器没有问题,显然浏览器也不会懂 phpthink 的 你的客户端只是模拟了浏览器的行为,自然也不需要懂 phpthink
加载更多回复(12)

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧