Session_start用户配置文件详细信息

时间:2013-12-14 14:39:06

标签: php mysql database

我正在学习php。我创建了一个注册和登录系统。它工作正常。但我对某些事情感到好奇:如何在用户个人资料中显示用户详细信息。例如姓名,姓氏,电子邮件等。我的登录系统功能正常,但只显示电子邮件。

这是我的profile.php代码。

<?php 

  session_start();
if (!isset($_SESSION['email'])) {

    header('location: index2.php');

    } 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link href="css/profile.css" rel="stylesheet" type="text/css" />


</head>

<body>
<div id="header">
<div class="header_in">
  <div class="logo"><span>EcoWebTr</span></div>
  <div class="cik"><a href="cikis.php">Çıkış</a></div>
  <!--Login informtion--></div>
</div>
<div class="container">
<div class="wrapper">
User Email:
<font color="#000066"><?php echo $_SESSION['email']; ?></font>

</div>
</div>

</body>
</html>

这也是login.php

<?php
include("includes/connect.php");

if(isset($_POST['login'])) {

  $email = $_POST['email'];
  $password  = md5($_POST['password']);

  $check_user = "SELECT * FROM users WHERE email='$email' AND password='$password'";

  $run = mysql_query($check_user);

  if(mysql_num_rows($run)>0) {
    $_SESSION['email']=$email;


    echo"<script>window.open('profile.php','_self')</script>";

  } else {

    echo"<script>alert('wrong email or password')</script>";

  }

}
?>

3 个答案:

答案 0 :(得分:0)

您可以采取几种不同的方法。

一种可能性,因为你说你的登录工作正常,我发现你看到电子邮件进入会话变量(大概是登录时)也是为其他字段设置会话变量你想要展示。

因此,将名字,姓氏,电子邮件等全部设置为会话变量。然后,在您的HTML中,您将能够根据需要引用它们。例如,<?php echo $_SESSION['first name']; ?>

<强>更新

鉴于您提供的其他信息,这里有一个简短的例子......

if(mysql_num_rows($run)>0) {
  $_SESSION['email']=$email;

  $row = mysql_fetch_assoc($run);
  $_SESSION['firstname'] = $row['firstname'];
  $_SESSION['lastname'] = $row['lastname'];
  // etc

此示例假定您将数据库中的名字/姓氏存储为“firstname”和“lastname”。只需根据需要调整名称即可匹配您的数据库。这个创建所有会话变量......然后您可以根据需要访问它们,就像访问您的电子邮件会话变量一样。

答案 1 :(得分:0)

您需要通过数据库查询它。我建议您在登录时查询所有内容并将其保存为会话变量,然后在任何地方回显。

像查理所说,你可以echo $_SESSION['full_name']。但是我通常会创建一个名为User的关联数组,然后将信息放入其中。例如,echo $_SESSION['USER']['full_name']听起来更合理。

示例

登录成功时:

1)查询姓名,姓氏,性别,生日等基本信息。

2)将它们保存在$ first_name,$ last_name等变量中。

3)将这些变量分配给这样的会话:

$first_name = $_SESSION['User']['first_name'];
$birthday = $_SESSION['User']['birthday'];

注销时,只需使用session_destroy()销毁会话。

答案 2 :(得分:-1)

类似的东西:

<div style="float:left;">User Name:</div><div style="margin-left:100px;"><?php=$_SESSION['username']?></div>
<div style="float:left;">User Surname:</div><div style="margin-left:100px;"><?php=$_SESSION['surname']?></div>

其他会话变量名称是什么?