简体   繁体   English

使用会话获取行并显示

[英]Fetch row and display using sessions

been working on this project where I want people to login and when successful see info that we get from the database.一直致力于这个项目,我希望人们登录并在成功时查看我们从数据库中获得的信息。 I am not sure how to get the user email and other data stored to session我不确定如何将用户电子邮件和其他数据存储到会话中

My problem is that I am struggling to store the data in sessions.我的问题是我正在努力将数据存储在会话中。 I can sign in and echo the username that I created a session for, but the other data is not working.我可以登录并回显我为其创建会话的用户名,但其他数据不起作用。

I have gone through stacks of stuff on here, but obviously I am a little lost.我在这里浏览了成堆的东西,但显然我有点迷失了。

Here is the code for my checklogin.php这是我的 checklogin.php 的代码

<?php

session_start();
ob_start();
include_once 'config.php';

// Connect to server and select databse.
try
{
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $db = new PDO('mysql:host='.$host.';dbname='.$db_name.';charset=utf8', $username, $password);
}
catch(Exception $e)
{
    die('Error : ' . $e->getMessage());
}   

// Define $myusername and $mypassword 
$myusername = $_POST['myusername']; 
$mypassword = $_POST['mypassword'];
$myemail = 

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$stmt = $db->query("SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");


// rowCount() is counting table row
$count = $stmt->rowCount();

// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1){

    // Register $myusername, $mypassword and print "true"
    echo "true";
    $_SESSION['username'] = $myusername;
    $_SESSION['email'] = $myemail;

}
else {
    //return the error message
    echo "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Wrong Username or Password</div>";
}

ob_end_flush();

?> ?>

and then my index.php looks like this然后我的 index.php 看起来像这样

<?php
  session_start();
  if(!isset($_SESSION['username'])){
    header("location:main_login.php");
  }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/main.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
  <div class="form-signin">
    <div class="alert alert-success">You have been <strong>successfully</strong> logged in as <?php echo $_SESSION['username']; ?>. Your email is <?php echo $row['email']; ?></div>





    <a href="logout.php" class="btn btn-default btn-lg btn-block">Logout</a> </div>

</div>
<!-- /container -->
</body>
</html>

Note that on index.php the username works fine when I echo it, but not the email.请注意,在 index.php 上,当我回显时,用户名工作正常,但不是电子邮件。

Any help would be appreciated任何帮助,将不胜感激

You can use query here to get all data for the session user, by select query.您可以在此处使用查询通过选择查询获取会话用户的所有数据。

$q= "select * from user where username = '$yoursessionusername'"

By this query you will get all details for the loggedin user.通过此查询,您将获得登录用户的所有详细信息。

Can create one session.php with below code:可以使用以下代码创建一个 session.php:

session_start(); session_start();

$username=$_SESSION['username'];
$email=$_SESSION['email'];

and can use those variables to your page.并且可以将这些变量用于您的页面。

This is supposed to be a comment, but i have a low reputation here.这应该是评论,但我在这里的声誉很低。

First of all, please consider binding your values.首先,请考虑绑定您的价值观。 PDO luckily has that functionality.Also as an observation, stripslashes does not totally prevent SQL injection.幸运的是,PDO 具有该功能。另外,作为观察,stripslashes 并不能完全防止 SQL 注入。

From what i see, your email address is not saved in any session variable.据我所知,您的电子邮件地址未保存在任何会话变量中。 This should fix,这应该解决,

$myemail = $_SESSION['name_of_email_from_database'];

Also do not forget, to access your session variables, include session_start() on every page that needs it.另外不要忘记,要访问您的会话变量,请在需要它的每个页面上包含 session_start()。 To prevent PHP's warning message of too many session starts, add the following code:为了防止 PHP 出现过多会话启动的警告信息,添加以下代码:

<?php
if(!isset($_SESSION)){
  session_start();
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM