dousenjue3214 2016-06-30 21:45
浏览 25
已采纳

使用PHP session_id和帐户系统[关闭]

So, I started to develop a website on my local server...

I wrote a Register/Login system with a MySQL database, in PHP. I know that i have to use sessions for it, but i dont really understand them.

  1. If i just start a session without an ID, it will generate a random ID?
  2. As I think, I should generate the ID and start the session after the SQL checking (pw and username) but before it throws the user to the next page. Am I right?
  3. Once I generate it as I mentioned in those questions, Should I store it in a SQL table or something?
  4. Will they die automatically or I have to kill them somehow? (except the manual logout page by the user click) I've read somewhere that they will die by the time, but as I think I have to set it up somehow.
  5. Where are they??? (I know, this is a highly retarded question) They are in the client or the server?

Yeah... I know, I should do something else... Well, I just need a source where they are documented for this kind of usage, or a description like this: Generate -> store -> check every time when the page changes -> they will kill themself automatically.

  • 写回答

1条回答 默认 最新

  • douyabu1528 2016-06-30 21:55
    关注

    Basically, you start the session on every page. The session is initialised on the first page the user visits. A session ID is generated whenever the session is initialised, so having a session ID as a login identifier is no good. The session itself, like all PHP code, is processed server-side.

    So every page should have session_start() at the very top.

    The thing you want to do is store session variables, for example $_SESSION['username'], and check whether the user is logged in or not with something in the trend of if(!empty($_SESSION['username'])

    Session die whenever you call 'session_abort()' or the browser is closed. You can also call session_set_cookie_params(3600,"/"); or something similar for a session duration (before session_start();), where the 3600 is the number of seconds the session is active.

    So one simple solution could be:

    session.php

    session_start();
    if(!empty($_SESSION['username') {
      echo "Hello";
    } else {
      echo $loginform;
    }
    

    randompage.php

    include "session.php";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline