简体   繁体   English

我想在他们登录后显示在线用户

[英]I want to display the online user when they have logged on

I would like to display: Logged in as: _ __ _ ___ 我想显示:登录为: _ __ _ ___

The code so far: 到目前为止的代码:

The login check, 登录检查,

<?php
include('config.php');

// username and password sent from form 
$myemail=$_POST['myemail']; 
$mypassword=$_POST['mypassword'];

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

$sql="SELECT * FROM $tbl_name WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

if($count==1){
session_start(); 
$_SESSION['myemail'] = $myemail; 
header("Location: http://www.jblanksby.yourwebsolution.net/login_success.php?            user=$myemail"); } 
else 
{ header("Location: http://www.jblanksby.yourwebsolution.net/loginerror.php"); 
} 
?>

The login success page/first members page, 登录成功页面/第一个成员页面,

<? 
$email = $_GET['myemail']; 
session_start(); 
$_SESSION['myemail'] = $email;  

if(isset($_SESSION['email'])){ 
} else { 
echo " 
<script language='javascript'> 
alert('Sorry, but you must login to view the members area!') 
</script> 
<script> 
window.location='http://jblanksby.yourwebsolution.net/sign_in.php' 
</script> 
"; } 
?>

<html>
blah blah blah
</html>

The code used to display the users email, 用于显示用户电子邮件的代码,

Logged in as: <? echo "$email"; ?>

The log in side of things is perfect. 登录方面的事情是完美的。 Just displaying the users email is proving difficult. 仅显示用户电子邮件证明是困难的。 What have I done wrong/missed out? 我做错了什么/错过了什么?

Since you are trying to display values out from the session, you have to use $_SESSION['email'] . 由于您尝试从会话中显示值,因此必须使用$_SESSION['email']

session_start(); // Or, if you have done it ahead, then omit this
echo "Logged in as:".$_SESSION['email'];

Use Starx suggestion on the $_SESSION but you need to move your session_start as the first line of PHP on the page, above $email = $_GET['myemail']; $_SESSION上使用Starx建议,但你需要将session_start作为页面上的第一行PHP移动,在$email = $_GET['myemail']; (or $email = $_SESSION['email'] ) (或$email = $_SESSION['email']

<?php
session_start();
$email = $_SESSION['email'];
?>

ADDED 添加

To use the session variable, declare it like so (assuming you are using a method=post on form submittal): 要使用会话变量,请将其声明为(假设您在表单提交时使用method = post):

<?php
session_start();
$_SESSION['email'] = $_POST['email'];
?>

This of course isn't safe for user input, it's presumed that you are sanatizing the user input prior to passing it from page to page and injecting it into your queries, this is just a simple example. 这当然对于用户输入是不安全的,假设您在将用户输入从一个页面传递到另一个页面并将其注入查询之前对其进行了静态处理,这只是一个简单的示例。

Login Script: 登录脚本:

$username = $_POST['username'];
$password = $_POST['password'];
$password = sha1($password);

if ($username&&$password) {
        require_once('../db/nstDBconnector.php');
        $qr = "SELECT * FROM `users` WHERE `username` = '$username'";
        $query = mysql_query($qr);
        $numrows = mysql_num_rows($query);

        if ($numrows!=0) {
            // check password against the password in the database
            while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
                $storedUsername = $row['username'];
                $storedPassword = $row['password'];
                $storedUserType = $row['user_type'];
                $storedUserFName = $row['full_name'];
                $_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
            }
            // check to see if username and password match
            if ($username==$storedUsername&&$password==$storedPassword) {
                ob_start();
                session_start();
                $_SESSION['username'] = $storedUsername;
                $_SESSION['user_type'] = $storedUserType;
                $_SESSION['full_name'] = $storedUserFName;
                header ('Location: main.php?login=successful');
            } else {
                header ('Location: index.php?error=incorrectPassword');
            }
        } else {
            header ('Location: index.php?error=noUser');
        }
    } else {
        header ('Location: index.php?error=missingUserAndPass');

    }

The page you are taking them to after login: 登录后您带他们去的页面:

if ($_SESSION['username']) {
    $userLogged = $_SESSION['username'];
    $userType = $_SESSION['user_type'];
    $userFName = $_SESSION['full_name'];
} else {
    header ('Location: http://www.yourdomain.com/index.php?error=notLoggedIn');
}

Where you want to show user name on the page: 您要在页面上显示用户名的位置:

Logged in as <?php echo $userFName; ?>.

or 要么

echo "Logged in as ", $userFName, ".";

暂无
暂无

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

相关问题 我要在登录时显示欢迎用户名的消息 - I want to display the welcome username's message when logged in 当我登录时,我想在 Laravel 中查看除登录用户以外的所有用户信息? - When I am logged in I want to see all users' information, other than the logged-in user, in Laravel? 登录用户后是否需要保存会话? - Do I have to save the session when the user is logged? 我想在发票状态更新/更改时向当前登录的用户发送邮件我该怎么做? - i want to send a mail to the currently logged in user when the invoice status is updated/changed how can i do it? 当我键入URL home.php时,它将打开,除非用户登录,否则我想阻止此操作 - when i type in the url home.php it opens, i want to prevent this action except if the user logged in 当我想让用户登录时,我收到“消息”:“未验证” laravel 护照 api - I get “message”: “Unauthenticated” laravel passport api when I want to get user logged in 仅在用户未登录Codeigniter时显示登录 - Display login only when user is not logged in Codeigniter 用户“ admin”登录时显示编辑按钮 - Display edit button when user “admin” logged in 当我有 $request->all() 时,将登录用户的 ID 保存到数据库 - Save logged in user's id to database when I have $request->all() 我想检查用户是否未使用PHP登录,然后使用jQuery将类添加到选择器 - I want to check if user is not logged in with PHP and then add a class to selector with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM