解决方案 »

  1.   

    sessionid 是服务端产生的,客户端只能读取
    不然如何能沟通呢?
      

  2.   

    你在客户端读呀!
    你给的链接里不是有读取 cookie 的部分吗?
      

  3.   

    还要把读到的 cookie 发回去,他也应该是有的
      

  4.   

    读到了,也发回去了,但是最后打开网页的时候,他又重新生成了一个session,这个sessionid和我发的不一样,而且无论怎么弄她都是固定的id
      

  5.   

    那你的 php 部分是怎么写的?
    如果“无论怎么弄她都是固定的id”,那你读到的和发回去的不也是那个固定的id吗?
      

  6.   

    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");
      

  7.   

    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; }
        }
    }
      

  8.   

    登陆页面
    <!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>
      

  9.   

    打印出你获取到的 cookie 的值
      

  10.   

    o8ru11js96qn9rneqokpc6alo2是这个吗?
      

  11.   

    GuestName|s:5:"admin";UserId|N;这个?
      

  12.   

    说错了,要同时打印 cookie 变量名和值
    tp 的表单有可能使用了 token,也要处理的模拟登录的流程是:
    1、访问登陆页,获取表单元素名和值(已知的话可跳过)
    2、获取 cookie、获取可能存在的 token
    3、获取可能存在的验证码图片并解析。如果有验证码,还需重新获取 cookie
    4、发送应提交的数据集合和 cookie 到表单目标页
      

  13.   

    admin:admin
    password:424
    __hash__:9dcee4d5bcfb44270f8cbfaebbc2d786_a57a03650a2256a207ed4f6ccaad6bf0
    没有验证码,这个__hash__是您指的token吗?
      

  14.   

    应该是你贴出的数据中并没有 sessionid
    你怎么总是取检查 session 呢?方向错了
      

  15.   

    o8ru11js96qn9rneqokpc6alo2这个是sessionid 但是是随机的 网站自己生成的sessionid是固定的。
    哪个方向错了啊!大神多指点指点,不懂呢
      

  16.   

    如果有 session 那么他在 cookie 数据中表现为
    PHPSESSID:.................
    你打印出的数据中没有这样的内容
      

  17.   

    c#不懂,但你每次连接服务器服务器都会重新建立一个session(服务器端配置文件里面设置的?),但是你的客户端这边好像并没有再次接受这个sessionid,还是使用以前的sessionid,你在程序中写上当退出页面的时候删除cookie行不行呢
      

  18.   

    谢谢大神,仔细思考了一下,我那些数据是通过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__内容。
    我的问题出在哪里呢?
      

  19.   

    之前的php配置文件我从来没动过,后来修改了一下,不好使然后又改了回去删除cookie肯定也是不行的撒,那个固定的cookie根本就没有用户的信息。
      

  20.   

    没改的话php中的session是概率刷新的,默认只有千分之一,现在phpthink那边每次都会随机产生,估计配置文件里面设置了每次重新打开网站session百分之百刷新
    你打开php.ini 看一下session.gc_probability和session.gc_divisor的值分别是多少,这两个值影响session的刷新概率
    现在你估计只要把里面的session.gc_probability设为0就能保证session不刷新
      

  21.   

    sessionid 为 60k402oc795mpvihq9jodpnml7 时
    生成的临时文件为 sess_60k402oc795mpvihq9jodpnml7
    至于里面是什么内容,则是有你的程序决定的,与旁人无关此时 cookie 中会有 PHPSESSID:60k402oc795mpvihq9jodpnml7 项
    如果你的客户端程序没有读到他,就表示你的客户端程序有问题
    鉴于你的客户端是 C# 的,你应该到 .net 版面去求解,显然那边 C# 的水平高于这里
      

  22.   

    是的 php后台登录是没问题的,只是对phpthink一点都不懂,所以才来这边问问。
      

  23.   

    既然用浏览器没有问题,显然浏览器也不会懂 phpthink 的
    你的客户端只是模拟了浏览器的行为,自然也不需要懂 phpthink
      

  24.   

    那phpthink是怎么运行的?浏览器只懂html吗?
      

  25.   

    是的,浏览器只懂html
    嗯,还懂 js