如何使用PHP实现登录脚本

如何使用PHP实现登录脚本,php,Php,我已经找到了这个登录脚本,我正在尝试将它实现到我的网站中,但遇到了一点问题: // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("testing") or die(mysql_error()); //checks cookies to make sure they are logged in if(isset($_COOKIE['I

我已经找到了这个登录脚本,我正在尝试将它实现到我的网站中,但遇到了一点问题:
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("testing") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
    $username = $_COOKIE['ID_my_site'];
    $pass = $_COOKIE['Key_my_site'];

    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or      die(mysql_error());

    while($info = mysql_fetch_array( $check ))
    {
        //if the cookie has the wrong password, they are taken to the login page
        if ($pass != $info['password'])
        {
            header("Location: loginscript.php");
        }

        //otherwise they are shown the admin area
        else
        {
            echo "<a href=logout.php>Logout</a>";
        }
    }
}
else
    //if the cookie does not exist, they are taken to the login screen
{
    header("Location: newlogin.php");
}
我想摆脱:

        else 

        { 

     echo "<a href=logout.php>Logout</a>"; 

        } 
由于它干扰了我的css,我只希望else语句的最后一部分正常工作,但当我取出它时,它会出错

我该怎么做


谢谢

您应该可以删除突出显示的部分

else 
{ 
echo "<a href=logout.php>Logout</a>"; 
} 

我不能质疑这作为身份验证/登录脚本的有效性,但这应该运行良好:

<?php
    // Connects to your Database 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("testing") or die(mysql_error()); 

    //checks cookies to make sure they are logged in
    if(isset($_COOKIE['ID_my_site']))
    { 
        $username = $_COOKIE['ID_my_site']; 

        $pass = $_COOKIE['Key_my_site']; 

        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or      die(mysql_error()); 

        while($info = mysql_fetch_array( $check ))
        {
            //if the cookie has the wrong password, they are taken to the login page 
            if ($pass != $info['password']) 
            {
                header("Location: loginscript.php"); 
            }
            //else //otherwise they are shown the admin area 
            //{ 
            //  echo "<a href=logout.php>Logout</a>"; 
            //} 
        }
    }
    else //if the cookie does not exist, they are taken to the login screen 
    {
        header("Location: newlogin.php");
    } 

?>

我冒昧地重新格式化了您在评论中发布的代码:

<?php 
    // Connects to your Database 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("testing") or die(mysql_error()); 
    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) 
    {
        $username = $_COOKIE['ID_my_site']; 
        $pass = $_COOKIE['Key_my_site']; 
        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
        while($info = mysql_fetch_array( $check ))   
        { 
            //if the cookie has the wrong password, they are taken to the login page 
            if ($pass != $info['password']) 
            {
                header("Location: loginscript.php"); 
            } 
            //otherwise they are shown the admin area    
            else 
            //if the cookie does not exist, they are taken to the login screen 
            {            
                header("Location: newlogin.php"); 
            } 
?>
如您所见,对于while循环或顶级if语句,您永远不会关闭花括号。因此,您的错误是:

分析错误:语法错误,第207行的E:\EasyPHP-5.3.9\www\test.php中出现意外的$end

我可以给您一个提示,当语法错误引用代码中的某个点时,请立即在该点之前查找实际问题。解析器得到的导致错误的点是意外的部分,这意味着在它被破坏之前


当然,始终正确设置代码的间距和缩进格式。它使这些事情更容易跟踪和纠正。

似乎您正在删除语句的结束块。代码的修改版本是什么,错误是什么?提示:修复代码缩进可能有助于识别语法错误,如不匹配的括号。@david这是修改后的代码:错误消息是:Parse error:syntax error,在第207行的E:\EasyPHP-5.3.9\www\test.php中意外出现$end,这只是一个HTML标记。我想我是在尝试删除parenthsis的,这同样有效,谢谢。那很有帮助。我正在努力学习,但这对我来说是全新的!
<?php
    // Connects to your Database 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("testing") or die(mysql_error()); 

    //checks cookies to make sure they are logged in
    if(isset($_COOKIE['ID_my_site']))
    { 
        $username = $_COOKIE['ID_my_site']; 

        $pass = $_COOKIE['Key_my_site']; 

        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or      die(mysql_error()); 

        while($info = mysql_fetch_array( $check ))
        {
            //if the cookie has the wrong password, they are taken to the login page 
            if ($pass != $info['password']) 
            {
                header("Location: loginscript.php"); 
            }
            //else //otherwise they are shown the admin area 
            //{ 
            //  echo "<a href=logout.php>Logout</a>"; 
            //} 
        }
    }
    else //if the cookie does not exist, they are taken to the login screen 
    {
        header("Location: newlogin.php");
    } 

?>
<?php 
    // Connects to your Database 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("testing") or die(mysql_error()); 
    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) 
    {
        $username = $_COOKIE['ID_my_site']; 
        $pass = $_COOKIE['Key_my_site']; 
        $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
        while($info = mysql_fetch_array( $check ))   
        { 
            //if the cookie has the wrong password, they are taken to the login page 
            if ($pass != $info['password']) 
            {
                header("Location: loginscript.php"); 
            } 
            //otherwise they are shown the admin area    
            else 
            //if the cookie does not exist, they are taken to the login screen 
            {            
                header("Location: newlogin.php"); 
            } 
?>