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

时间:2017-03-22 13:12:28

标签: php html mysql

第一手非常感谢您提供的帮助,我正在尝试创建一个带有porfile部分的登录部分,登录工作很棒,但我希望获得刚刚登录的用户的所有信息页面并将其显示在配置文件部分,我该怎么做。这是index.php的文件(只需获取页面的语言并重定向到correcto版本并检查用户是否登录),gallery.php(profile section)和login.php(检查用户是否DB),home-de.php(就在登录表格的地方)。

数据库很简单,只需一个表调用用户和字段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.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 个答案:

答案 0 :(得分:-2)

在login.php中:

$_SESSION['users'] = $user_email;
$_SESSION['user_id'] = $result['id'];
$_SESSION['user_first_name'] = $result['first_name'];
$_SESSION['user_last_name'] = $result['last_name'];

in de / gallery_de.php

<?php echo $_SESSION['user_first_name'];?>

答案 1 :(得分:-2)

在Login.php中

$result = $statement->fetch();

之后

这样做

 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>';
  }

然后在 de / gallery_de.php

替换

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

<?php echo $_SESSION[user_name] ?>