没有显示问候消息

时间:2015-01-28 19:47:53

标签: php

当我意识到我遇到了问题但我不知道它是什么时,我正在登录我的登录系统: 在index.php页面中,标题应该显示一个欢迎访客并链接到登录和注册页面...如果阵风登录显示欢迎$ username例如。 然而,当我测试它...如果我登录它重定向到index.php页面,因为我输入代码但msg不会改变... 这是代码: index.php / header.php:

    <?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
session_start();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>

<LINK REL=StyleSheet HREF="css/test-style.css" TYPE="text/css">

</head>

<body id="body">

<div id="header">
<div id="Greeting">

<?php

       if ($userid && $username) {

              echo "<p>Welcome <b>$username</b></p></p><a href='logout.php'>Logout</a></p>";

       } else {

              echo "<p>Welcome Guest <br><a href='login.php'>Log in</a> | <a href='register.php'>Register</a></p>";

       }

?>

</div>
<div id="logo"><h2>Testing website</h2></div>
</div>

<div id='cssmenu'>
<ul>
   <li><a href='#'><span>Test link 1</span></a></li>
   <li><a href='#'><span>Test link 2</span></a></li>
   <li><a href='#'><span>Test link 3</span></a></li>
   <li><a href='#'><span>Test link 4</span></a></li>
</ul>
</div>

<div>

这是login.php页面:

    <?php
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(0);
include 'includes/header.php';
?>

<div id="login">
<h2>Log in</h2>

<?php  

    if ($_POST['loginbtn']) {
        $user = $_POST['user'];
        $password = $_POST['pass'];
            if ($user) {
                if ($password) {
                    require ("core/connect.php");
                    $query= mysql_query("SELECT * FROM users WHERE username='$user'");
                    $numrows = mysql_num_rows($query);
                        if ($numrows == 1) {
                            $row = mysql_fetch_assoc($query);
                            $dbid = $row['id'];
                            $dbuser = $row['username'];
                            $dbpass = $row['password'];
                            $dbactive = $row['active'];
                                if ($password == $dbpass){
                                if ($dbactive == 1) {
                                    session_start();
                                    $userid = $_SESSION['userid'];
                                    $username = $_SESSION['username'];
                                    header('Location: index.php');
                                  } else {
                                   echo "<font color='red'>You must activate your account to login.</font>";
                                  }
                                 }else {
                                  echo "<font color='red'>You've entered an invalid username or password.</font>";
                                 }
                           }else{
                           echo "<font color='red'>You've entered an invalid username or password.</font>";
                           }
                           mysql_close();
                    }else{
                    echo "<font color='red'>You must enter a password.</font>";
                    }
                } else {
                echo "<font color='red'>You must enter a username.</font>";
                }
}
?>

<form action="index.php" method="POST">
<font color="black">Username: </font><br><input type="text" name="user"><br><br>
<font color="black">Password: </font><br><input type="password" name="pass"><br><br>
<input type="submit" value="Login" name="loginbtn" />
<br><br>
<font color="black">Don\'t have an account ? <a href="register.php">Register</a></font>
</form>
</div>';

<?php include 'includes/footer.php'; ?>

感谢阅读。

2 个答案:

答案 0 :(得分:0)

在您的login.php中,您使用了以下内容: $userid = $_SESSION['userid']; $username = $_SESSION['username']; session_start()

之后

你应该在之前初始化它们,如下所示:

$userid = $dbid; $username = $dbuser;

他们可以设置 $ _ SESSION

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

答案 1 :(得分:0)

login.php替换

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

$_SESSION['userid'] = $dbid;
$_SESSION['username'] = $dbuser;

您实际上从未设置会话变量。上面这两行就是这样做的。

你的脚本需要做很多工作,但我只是回答这个问题。继续学习这是唯一的方法!