PHP登录和聊天

时间:2015-12-30 12:46:18

标签: php xampp chat

我是PHP的新手,我没有那么多经验。

我有两个问题:

首先,我正在尝试使用XAMPP进行登录页面,这不起作用,它非常基本,如您所见:

test2.php

<?php
    $connection = mysql_connect("localhost","root","") or die("Couldnt connect to the server!");
    mysql_select_db("testdb",$connection) or die("Couldnt connect to the database!");

    error_reporting(0);

    if ($_POST['login']){
        if ($_POST['username'] && $_POST['password']){
            $username = mysql_real_escape_string($_POST['username']);
            $password = mysql_real_escape_string(hash("sha512", $_POST['password']));
            $user = mysql_fetch_array(mysql_query("SELECT * FROM 'users' WHERE 'Username'='$username'"));
            if ($user == 0){
                die("That username doesn't exist! Try making <i>$username</i> today! <a href='test2.php'>&larr;                             Back</a>");
            }
            if ($user['Password'] != $password){

                die("Incorrect password! <a href='test2.php'>&larr; Back</a>");
            }
            $salt = hash("sha512",rand().rand().rand());
            setcookie("c_user",hash("sha512",$username),time()+24*60*60,"/");
            setcookie("c_salt",$salt,time()+24*60*60,"/");
            $userID = $user['ID'];
            mysql_query("UPDATE 'users' SET 'Salt'='$salt' WHERE 'ID'='$userID'");
            die("You are now logged in as $username!");

        }
    }

include "algor.php";
if ($logged == true){
    die("You're already logged in");
}
    echo " 

        <body style='font-family:verdana, sans-serif;'>
            <div style='width: 80%; padding: 10px; border:1px solid #e3e3e3; background-color: #fff; color: #000;'>
                <h1> Login </h1>
                <br/>
                <form action='' method='post'>
                    <table>
                        <tr>
                            <td>
                                <b> Username: </b>
                            </td>
                            <td>
                                <input type='text' name='username' style='padding: 4px;'/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b> Password: </b>
                            </td>
                            <td>
                                <input type='password' name='password' style='padding: 4px;'/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type='submit' value='Login' name='login'/>
                            </td>
                        </tr>

                    </table>
                <form>
                <br/>
                <h6> No account? <a href='register.php'> Register! </a> </h6>
            </div>
        </body>
    ";
?>

Register.php

<?php

$connection = mysql_connect("localhost","root","") or die("Couldnt connect to the server!");
mysql_select_db("testdb",$connection) or die("Couldnt connect to the database!");

error_reporting(0);

if ($_POST['register']){
        if ($_POST['username'] && $_POST['password']){
            $username = mysql_real_escape_string($_POST['username']);
            $password = mysql_real_escape_string(hash("sha512", $_POST['password']));
            $name = '';
            if ($_POST['name']){
                $name = mysql_real_escape_string(strip_tags($_POST['name']));
            }
            $check = mysql_fetch_array(mysql_query("SELECT * FROM 'users' WHERE 'Username' = '$username'"));
            if ($check != 0){
                die("That username already exist! Try <i>$username".rand(1,50)."</i> insted! <a href='register.php'>                        &larr; Back </a>");
            }
            if (!ctype_alnum($username)){
                die("Username contains special charcters! Only numbers and letters are premitted! <a                                      href='register.php'> &larr; Back </a>");
            }
            if (strlen($username > 20)){
                die("Username must not contain more than 20 chatacters! <a href='register.php'> &larr; Back </a>");
            }
            $salt = hash("sha512",rand() . rand() . rand());
            mysql_query("INSERT INTO 'users' ('Username','Password','Name','Salt') VALUES ('$username','$password','$name','$salt')");
            setcookie("c_user",hash("sha512",$username), time() + 24 * 60 * 60,"/");
            setcookie("c_salt",$salt, time() + 24 * 60 * 60 ,"/");
            die("Your account has been created and you are now logged in.");
        }
}


echo "
     <body style='font-family:verdana, sans-serif;'>
            <div style='width: 80%; padding: 10px; border:1px solid #e3e3e3; background-color: #fff; color: #000;'>
                <h1> Register</h1>
                <br/>
                <form action='' method='post'>
                    <table>
                        <tr>
                            <td>
                                <b> Username: </b>
                            </td>
                            <td>
                                <input type='text' name='username' style='padding: 4px;'/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b> Password: </b>
                            </td>
                            <td>
                                <input type='password' name='password' style='padding: 4px;'/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b> Name: </b>
                            </td>
                            <td>
                                <input type='text' name='name' style='padding: 4px;'/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type='submit' value='Register' name='register'/>
                            </td>
                        </tr>
                    </table>
                </form>    
";    
?>

我的第二个问题是如何在两个人之间进行像Facebook这样的聊天对话。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您是否为my_sql设置配置了xampp,如果还没有,请在php.ini中进行更改

删除;从这两行开始。

;延长=激活php_mysql.dll ; extension = php_mysqli.dll