-1

我早些时候在我的注册页面上得到了一些帮助,但现在我无法让我的登录页面正常运行。

这是登录代码:

<?php

    session_start();

    if(isset($_SESSION['user']))
    {
        echo "WELCOME you are logged in";   
    }

    if(isset($_POST['user']) && isset($_POST['password']))
    {
        $user = mysql_real_escape_string($_POST['username']);
        $password = shal1($_POST['password']);

        $db = connect('jsnow_jsnow','missysnow2005');
        $eval = validate($db,$username,$password);
    }

    if($eval!=false)
    {
        echo "Welcome ".$eval; 
        $_SESSION['username']=$eval;
    }


    function connect($dbuser, $dbpassword)
    {
        try
        {
            $db = new PDO('mysql:host=localhost;dbname=jsnow_members', 'jsnow_jsnow', '*******');
            return $db;
        }
        catch(PDOException $e)
        {
            echo $e;
            return false;
        }
    }

    function validate($db, $user,$password)
    {
        $username=false;
        $query = "Select username,password FROM jsnow_members where username   ='".$user."' AND password = '".$password."'";
        try
        {
            $db->beginTransaction();
            $result = $db->query($query);

            foreach($result as $row)
            {
                $username = $row['username'];   
            }
            $db->commit();

            return $username;
        }
        catch(Exception $e) {}
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <form action="http://vps8383.inmotionhosting.com/~jsnow/login.php">
            username: <input type="text" name="username"/>
            password: <input type="password" name="password"/>
            <input type="submit"/>
        </body>
    </html>

数据库名称是jsnow_members,其字段是username,passwordemail.

我从教程中获得了此代码,但显然效果不太好,我从同一个教程中获得了注册页面代码,并且这里的人们需要大量工作和建议才能使其正常工作。

这是注册页面的代码,它用注册信息填充数据库....

<?php

    if(isset($_POST['username']) && isset($_POST['password']))
    {
        $db =connect('jsnow_jsnow','******');
        if($db!=false)
        {
            register($db);
            echo "User registered";
        }     
    }

    function connect($dbuser, $dbpassword)
    {
        try
        {
            $db = new PDO('mysql:host=localhost;dbname=login',$dbuser,$dbpassword);
            return $db;
        }
        catch(PDOException $e)
        {
            echo $e;
            return false;
        }
    }

    function register($db)
    {
        $user  = mysql_real_escape_string($_POST['username']);
        $password = shal($_POST['password']);
        $email = mysql_real_escape_string($_POST['e_mail']);

        $query = "INSERT INTO members(username,password,email)   values('".$user."','".$password."','".$email."')";
        try
        {
            $db->eginTransaction();
            $db->exec($query);
            $db->commit();
            echo "commit succesful";
        }
        catch(Exception $e){}
    }
?>
<!DOCTYPE html> 
<html>
    <head></head>
    <body>
        <form action="http://localhost/projects/login/rigister/php" method="post">
            username:<input type="text" name="username"/>
            password:<input type="password" name="password"/>
            email<input type="text" name="e_mail"/>
            <input type="submit"/>
        </form>
    </body>
</html>
4

1 回答 1

0

改变这个:

if(isset($_SESSION['user']))
{
echo "WELCOME you are logged in";   
}

对此:

if(isset($_SESSION['username']))
{
echo "WELCOME you are logged in";   
}
于 2013-10-17T01:12:05.633 回答