简体   繁体   English

如何在Java中更改phpsessid?

[英]How to change phpsessid in java?

I have a java code that submits a multipart form on a site where I need to register. 我有一个Java代码,可以在需要注册的站点上提交多部分表单。 I've tested my script on various scripts and it works when the cookies on the site look like user=username or uid=username and so on, but id does not work when I have something like PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxx, I can't even change its value. 我已经在各种脚本上测试了我的脚本,当站点上的cookie看起来像user = username或uid = username等等时,它可以工作,但是当我有PHPSESSID = xxxxxxxxxxxxxxxxxxxxxxx之类的东西时,id不起作用,我什至不能改变它的价值。 Please help me! 请帮我!

connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
if(is_cookie){
connection.setRequestProperty("Cookie", cookie);
            }
            connection.setRequestProperty("USER-AGENT", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Accept-Charset","iso-8859-1,*,utf-8");
            DataOutputStream httpStreamWriter = new DataOutputStream(connection.getOutputStream ());

YES. 是。 YOU CAN! 您可以!

*but it depends on server configuration *但是取决于服务器配置

in my case (this is minimal code for educational purpose): 就我而言(这是出于教育目的的最小代码):

  • I create a session with my own PHPSESSID by login with that id 通过使用该ID登录,使用我自己的PHPSESSID创建会话
  • then i do a multi-part post 然后我做一个多部分的文章

     try { String charset = "UTF-8"; String cookie = "PHPSESSID=aohgsqanei5v2n6su95e0f4vq1"; //custom id URLConnection connection; InputStream response; // Construct the POST data for login change to desired param/value String data = "login=" + URLEncoder.encode(".....", charset) + "&password=" + URLEncoder.encode(".....", charset); byte[] dataBytes = data.getBytes(charset); String _url_login_post = "http://www.domain.com/index.php?action=login"; connection = new URL(_url_login_post).openConnection(); connection.setDoOutput(true); connection.addRequestProperty("Cookie", cookie); OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); outputStream.write(dataBytes); outputStream.flush(); response = connection.getInputStream(); response.read(); //Find the directory for the SD Card using the API //*Don't* hardcode "/sdcard" File sdcard = Environment.getExternalStorageDirectory(); File binaryFile = new File(sdcard, "404.jpg"); //must exist on sdcard String param = "content"; //form data param name String _message = "Message here..."; //form data value String delimeter = "------WebKitFormBoundary"; String delimeter_end = "--"; String boundary = Long.toHexString(System.currentTimeMillis()); //random String CRLF = "\\r\\n"; // Line separator required by multipart/form-data. String _url_mpart_post = "http://www.domain.com/index.php?action=user&mode="; connection = new URL(_url_mpart_post).openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary" + boundary); connection.addRequestProperty("Cookie", cookie); OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // Send normal param. writer.append(delimeter + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\\"" + param + "\\"").append(CRLF); writer.append(CRLF).append(_message).append(CRLF).flush(); // Send binary file. writer.append(delimeter + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\\"datafile\\"; filename=\\"" + binaryFile.getName() + "\\"").append(CRLF); writer.append("Content-Type: " URLConnection.guessContentTypeFromName(binaryFile.getName())) .append(CRLF); writer.append(CRLF).flush(); file.copy(binaryFile, output); output.flush(); // Important before continuing with writer! writer.append(CRLF).flush(); // CRLF indicates end of boundary. // End of multipart/form-data. writer.append(delimeter + boundary + delimeter_end).append(CRLF).flush(); writer.close(); response = connection.getInputStream(); response.read(); } catch (Exception e) { } 

The PHPSESSIONID is a ID genetated by PHP at serverside. PHPSESSIONID是PHP在服务器端生成的ID。 If you don't have that ID you may not sent any data to that PHP script, because the server 'identifies' the user by that ID. 如果您没有该ID,则可能不会向该PHP脚本发送任何数据,因为服务器会通过该ID“识别”用户。

To answer your question: you cannot change that ID. 要回答您的问题:您不能更改该ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM