php - 用户 session 和显示用户信息

标签 php html css session usersession

我正在做一个元素,进入管理页面需要用户名和密码。一旦用户名和密码被更正或重定向到其他页面, session 就会创建,如果其中任何一个不正确。 我的代码是:

<?php

    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]="username";
        $_SESSION["password"]="passwoed";
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


?>

在管理页面,我的代码是:

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>
</body>

问题是我被直接重定向到主页而无法进入管理页面。 session 注册有问题吗?但是,如果我将 php 代码更改为,我在管理页面上:

<?php
    session_start();    
    if(isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

我想显示我通过 $_SESSION[] 超变量保存的用户名。我收到的错误消息是 Notice: Undefined index: username in C:\xampp\htdocs\admin_page.php.

提前致谢


我按照你说的改了代码。

但是我是否写

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $username; ?></font>       </div>
</body>

我得到了同样的错误。 “注意:未定义索引:C:\xampp\htdocs\admin_page.php 中的用户名。” 我不知道下一步该怎么做。请建议!!!

最佳答案

当我将代码更改为:

<?php

    session_start();
    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]=$username;//instead of "username"
        $_SESSION["password"]=$password;
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


    ?>

谢谢 Geseft。

关于php - 用户 session 和显示用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409666/

相关文章:

html - 菜单按钮背景被页面图像背景覆盖,我该如何防止这种情况发生?

html - ListItem 圆盘显示在垂直底部

html - 审核复杂网页上未使用的 CSS

html - 仅 CSS 弹出窗口 - 页面在关闭时移动

php - Linux (centos) 上的 Codeigniter 类和文件名区分大小写

php - 只显示最新的 20 个 Sql 表条目?

java - 如何在 Google AppEngine 中将 HTML 转换为内联 CSS?

javascript - 咕噜声 : how to generate jshint output as HTML

php - 如何插入http ://to a website address whitch is inserted automatically with php

php - 如何在laravel中用Carbon解析一个伪造的dateTimeBetween