dongyilai4214 2017-03-22 13:12
浏览 314
已采纳

登录后获取用户信息以显示在个人资料部分

first hand thanks so much for the help you could give, i´m trying to create a login section with a porfile section, the login work great, but i want to get all the information of the user that just login in to the page and display it on the profile section, how can i do that. Here are the file for the index.php(just get language of the page and redirect to a the correcto version and check if the user is login or not), gallery.php(profile section) and login.php(check the user on the DB), home-de.php(just where loging form is).

The data base is simple just one table call users and fields id, first_name, last_name, email, password.

index.php

<?php
        session_start();
    $accept_language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $lang = $accept_language[0];
    echo $lang;
    switch ($lang)
    {
        case 'en':
                    if (isset($_SESSION['users'])) {
                        header('Location: en/gallery_en.php');
                    }else{
                        header('Location: en/home-en.php');
                    }
            break;
        default:
                if (isset($_SESSION['users'])) {
                    header('Location: de/gallery_de.php');
                }else{
                    header('Location: de/home-de.php');
                }
    }
?>

login.php

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

    $errores = '';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $user_email = strtolower($_POST['email']);
      $user_pass = $_POST['password'];

      try {
        $connection = new PDO('mysql:host=localhost;dbname=user_login', 'root', '');
      } catch (PDOException $e) {
        echo "Error:" . $e->getMessage();;
      }

      $statement = $connection->prepare('SELECT * FROM users WHERE user_email = :email AND user_pass = :password');
      $statement->execute(array(':email' => $user_email,':password' => $user_pass));

      $result = $statement->fetch();

      if ($result !== false) {
        $_SESSION['users'] = $user_email;
        header('Location: '. $location . '');
      } else {
        $errores .= '<li>Datos Incorrectos</li>';
      }
    }
  }

?>

de/home-de.php

<?php
    require ('../login.php');
    login('../de/gallery.php');
?>
          <form id="login-form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" name="login">
            <div class="login-field">
              <label for="email_login">Email</label>
              <input type="text" name="email" id="email_login">
              <div class="message text-center"><p>This field is required</p></div>
              <div class="pattern text-center"><p>Enter a valid email.</p></div>
            </div>
            <div class="login-field">
              <label for="pass_login">Password</label>
              <input type="password" name="password" id="pass_login">
              <div class="message text-center"><p>This field is required</p></div>
            </div>
            <input type="submit" value="Login">
          </form>

de/gallery_de.php

<?php
  session_start();
  if (isset($_SESSION['users'])) {

  } else {
    header('Location: ../index.php');
  }
?>

<h1 class="title-bar">Willkommen <?php echo '(here will put the first name of the user)'?></h1>
  • 写回答

2条回答 默认 最新

  • douliao1911 2017-03-22 13:21
    关注

    In Login.php

    after this $result = $statement->fetch();

    do this

     if ($result !== false) {
        $_SESSION['users'] = $user_email;
        $_SESSION['user_name'] = $result->firstName;  //you have change the field name 
        header('Location: '. $location . '');
      } else {
        $errores .= '<li>Datos Incorrectos</li>';
      }
    

    then in de/gallery_de.php

    replace

    <?php echo '(here will put the first name of the user)'?>

    with

    <?php echo $_SESSION[user_name] ?>

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

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab