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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?