douyan1882 2015-07-13 16:22
浏览 44
已采纳

与同一个表的元素相关的多个会话是否有问题?

I was looking through my registration and login form, and the thing that is bothering me is that I have several sessions defined for individual variables of each form. Here is what I am talking about:

Part of my registration form:

    $pass = md5($pass);
          $regSQLI = "INSERT INTO users (id, email, birth_date, first_name, last_name, password, sign_up_date, activated) VALUES ('','$em','$bday','$fn','$ln','$pass','$sud','0')";
          $regQuery = mysqli_query($con, $regSQLI);
          //variables that will be passed over from the register fields to forthcoming sessions
          $_SESSION["email_login"] = $em;
          $_SESSION["first_name"] = $fn;
          $_SESSION["last_name"] = $ln;

Part of my login form:

    if ($userCount == 1) { //if the search finds a matching record of the login input form
      while( $row = mysqli_fetch_assoc($sqli)) { // use fetch_assoc
          $id = $row["id"];
          }
      $_SESSION["email_login"] = $email_login;
      $_SESSION["first_name"] = $fn;
      $_SESSION["last_name"] = $ln;
      header("location: home.php");
      exit();
  }
  else {
    echo '<div id="regerrormsg">Login information is invalid. </div>';
  }
    }

Everything works, but I want to know whether I can save some performance by cleaning up having to evoke multiple sessions. The reason why I have multiple sessions is because even though users will be using their emails to register and login, the system will address/echo them by their names:

      <?php
    echo $_SESSION["first_name"]."&nbsp".$_SESSION["last_name"].".";
    ?>

Is there a way to where I can just have one session variable (perhaps email) as a validator and retrieve data from the database that corresponds with the session variable in question? Would that be cleaner and smoother? Thank you.

  • 写回答

2条回答 默认 最新

  • duanhongxian6982 2015-07-13 17:39
    关注

    Everything looks quite normal, and there are probably not much performance to gain by fiddeling.

    On top of that, your code doesn’t actually show what the user is asked to input via the form, or what the database actually is asked to search for ... which would be the points, that might be optimised a bit.

    ... and, it seems you misunderstand what the superglobal $_SESSION really is.

    What you view as 'several sessions', is in fact just one standard session: An array holding any number of key/value-pairs you would like to put in.

    When you call session_start();, the $_SESSION is automatically generated and/or loaded from file/memory/database (your save-handler), a cookie is set or a url-rewrite done with $_GET. All depending on your settings.

    Read more here: http://php.net/manual/en/intro.session.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 Ubuntu20.04无法连接GitHub
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线