如何使用PHP实现登录脚本

时间:2012-03-29 18:30:16

标签: 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
        {
            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语句的最后一部分工作,但是当我把它拿出来时就会出错。

我该怎么做?

感谢

4 个答案:

答案 0 :(得分:0)

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

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

如果不起作用,则可以尝试删除回显文本。

else 
{ 
echo ""; 
} 

答案 1 :(得分:0)

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"); 

}

 ?> 

答案 2 :(得分:0)

我无法对此作为auth / login脚本的有效性提出异议,但这应运行良好:

<?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");
    } 

?>

答案 3 :(得分:0)

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

<?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中的意外$结尾

我可以给你的一个提示是,当语法错误引用代码中的某个点时,请立即查看之前的指出实际问题。解析器产生错误的点是意外的部分,这意味着在它被破坏之前的某些东西。

当然,始终正确格式化代码以进行间距和缩进。它使很多更容易追踪和纠正。