dsvjw20866 2013-08-27 19:02
浏览 55
已采纳

查询行取决于登录

I'm trying to echo some data from a table which is only relevant to whoever is logged in. For example, if I have a table: ID-username-password-avatar and I want to display the avatar for the person that's logged in, how can I go about doing that? So far I have managed to echo the first avatar in the first row no matter who's logged in.

This is my session code:

<?php
session_start();
if (!isset($_SESSION['myusername']))
{
  header('Location:login.php');
}

This is my attempt at getting the row based on which user is connected using

  FROM login WHERE username='$name'"

  $conn = new PDO("mysql:host=MYHOST;dbname=MYDBNAME", "NAME", "PASSWORD");
  $name =  "demo"; $stmt = $conn->query("SELECT * FROM login WHERE username='$name'");
  $stmt->execute();
  $row = $stmt->fetch(PDO::FETCH_OBJ);
  $wm = array('avatar' => $row->avatar);
?>
  • 写回答

1条回答 默认 最新

  • dongyongyu0789 2013-08-27 19:13
    关注

    Put your name in the $_SESSION['myusername'] superglobal. Then you can fetch it from any page until the session's been destroyed.

    In your code:

    $_SESSION['myusername'] = $name;
    

    Similar to this, you can put an image path as well and display an avatar.

    Edit

    Just to improve my answer a little bit more, I'll give you a complete example of the login form.

    Let's say we have an (X)HTML form like in the following code, you can call it "login.php":

    <form action="login.php" method="post">
      <dl>
        <dt>Username:</dt>
          <dd><input type="text" name="username"/></dd>
        <dt>Password:</dt>
          <dd><input type="text" name="password"/></dd>
        <dt><input type="submit"/></dt>
      </dl>
    </form>
    

    Now when you have the (X)HTML code, you add the necessary PHP code:

    <?php
    
    if( isset($_POST["username"]) )
    {
      $username = $_POST["username"];
      $password = $_POST["password"];
      $connection = new mysqli("host", "name", "password", "dbname");
      $connection->set_charset("utf8");
      $username = $connection->real_escape_string($username); //Security
      $password = $connection->real_escape_string($password); //Security
      $result = $connection->query("SELECT * FROM login WHERE username LIKE BINARY '$username' AND password LIKE BINARY '$password'");
      if($result->num_rows==1)
      {
        $_SESSION['myusername'] = $username;
        $row = $result->fetch_assoc();
        $_SESSION['myavatar'] = $row["avatar"];
      }
      $connection->close();
    }
    
    ?>
    
    <form action="login.php" method="post">
      <dl>
        <dt>Username:</dt>
          <dd><input type="text" name="username"/></dd>
        <dt>Password:</dt>
          <dd><input type="text" name="password"/></dd>
        <dt><input type="submit"/></dt>
      </dl>
    </form>
    

    Now you open a page you want to protect with username and password and enter PHP code similar to the following. You can do with your avatar whatever you want on any page that has the session code.

    <?php
      session_start();
      if ( !isset($_SESSION['myusername']) )
      {
        header("Location:login.php");
      }
    
      echo $_SESSION['myavatar'];
    ?>
    

    Depending on the user that's logged in, a different avatar will be shown.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错