0

我正在尝试在我的用户登录后制作仪表板,但是我希望它在不同的页面上。但我担心通过 url 传递数据可能不是必需的,因为它可以手动更改。我想传递$username给我的 dash.php 文件。这是我的代码:

登录.php

<?php
session_start();
//Insert Connection String
require_once 'config.php';
if(!$_SESSION['username']){
if (!isset($_POST['submit'])) {
    echo'<form action="login.php?logged=yes" method="post">';
    echo'<label> Username</label>';
    echo'<input type="text" name="username"/>';
    echo'<label> Passowrd</label>';
    echo'<input type="password" name="password"/>';
    echo'<input type="submit" name="submit" value="Login!"/>';
    echo'</form>';
} else {


    //handle some errors
    //If both fields are empty
    if(!$_POST['username'] && !$_POST['password']) {
        echo"Try to login without entering any info, genius.";
    }
    else {
        //check if the username exists
        if(!empty($_POST['username'])) {
            //check if the password exists
            if(!empty($_POST['password'])) {
                //Put unencrypted username variable
               $username = $_POST['username'];
                //Encrypt the values
                $xusername = md5($_POST['username']);
                $xpassword = md5($_POST['password']);
                //Check if they exist in the database
                $query = odbc_exec($conn, "SELECT * FROM xmember WHERE    username='$xusername' AND password='$xpassword'");
                $user_rows = 0;
                while ($row = odbc_fetch_array($query)) {
                    $user_rows++;
                }
                odbc_free_result($query);
                if($user_rows == 1) {
                    echo 'Welcome, '.$_POST['username'];
                    $_SESSION['username'] = $_POST['username'];
                    echo "<meta http-equiv='refresh' content='3;url=dash.php'>";

                }
                else {echo"Sorry, your account information is invalid.";}
            }
            else {echo"Please put your password";}
        }
        else {echo"Please put your username";}

    }   


}
else {echo"what are you doing here?";}
?>

配置文件

<?php
/*
Le Change Nickname PHP v1.0 made by Thor KK Klein LOL
CONFIG section
*/
//Set Network Config
$odbc_dsn = "mydb";
$odbc_user = "sa";
$odbc_password = "wh@tTh3!?";

$conn = odbc_connect($odbc_dsn, $odbc_user, $odbc_password);
if(!$conn) {die('Failed to connect to the database!');}


?>

破折号.php

<?php
session_start();
require_once 'login.php';//load connection settings and get info

if(!$_SESSION['username']){
    echo"Are you kidding me?";
}else {
    //Display The Dashboard

    //Get user's typical information

    //Get user's table row array
    echo"Welcome, ".$username;
}
?>

我尝试对我的 dash.php 要求一次 login.php 来获取数据.. 但它似乎不起作用。

4

1 回答 1

1

Sinde 您将用户名存储在会话变量中,您可以从任何文件 usibg 访问它$_SESSION["username"]。如果未设置密钥,您可以将用户重定向到您的登录页面。
您的“dash.php”文件可以这样修改:

<?php
require_once("config.php");
session_start();

if(!isSet($_SESSION["username"])) {
    // Redirect user to login page
    header("Location: login.php");
    exit();
}else {
    //Display The Dashboard
    //Get user's typical information
    //Get user's table row array

    echo("Welcome, " . $_SESSION["username"]);
}
?>
于 2013-06-02T06:52:17.140 回答