注意:使用未定义的常量

时间:2011-08-17 09:58:52

标签: php mysql login

我遇到了这个登录脚本的问题(开源btw)。出于某种原因,我收到以下错误: 注意你在哪里看到“---”我已经删除了某些东西!如果有人帮助我,我将不胜感激:)

Notice: Use of undefined constant dfengineid - assumed 'dfengineid' in C:\xampp\htdocs\---\resources\login.php on line 113

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\---\index.php:217) in C:\xampp\htdocs\dfengine.com\resources\login.php on line 113

Notice: Use of undefined constant dfenginekey - assumed 'dfenginekey' in C:\xampp\htdocs\---\resources\login.php on line 115

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\---\index.php:217) in C:\xampp\htdocs\dfengine.com\resources\login.php on line 115

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\---\index.php:217) in C:\xampp\htdocs\dfengine.com\resources\login.php on line 121

 <?php 

// Connects to your Database 

 mysql_connect("localhost", "root", "---") or die(mysql_error()); 
 mysql_select_db("dfeuserdb") or die(mysql_error()); 

 //This code runs if the form has been submitted
 //Checks if there is a login cookie
 if(isset($_COOKIE['dfengineid']))
 //if there is, it logs you in and directes you to the members page
 { 
    $username = $_COOKIE['dfengineid']; 
    $pass = $_COOKIE['dfenginekey'];
    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

    while($info = mysql_fetch_array( $check ))  
    {
        if ($pass != $info['password']) {}
        else
        {
            header("Location: search.php");
        }
    }
 }

 //if the login form is submitted     
 if (isset($_POST['submit'])) { // if form has been submitted

    // makes sure they filled it in  
    if(!$_POST['username'] | !$_POST['pass']) {    
        die('You did not fill in a required field.');
    }

    // checks it against the database
    $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

    //Gives error if user dosen't exist
    $check2 = mysql_num_rows($check);
    if ($check2 == 0) {
        die('Username or Password is incorrect! Please <a href="index.php">try again</a>.');
    }

    while($info = mysql_fetch_array( $check )){
        $_POST['pass'] = stripslashes($_POST['pass']);
        $info['password'] = stripslashes($info['password']);
        $_POST['pass'] = md5($_POST['pass']);

    //gives error if the password is wrong

        if ($_POST['pass'] != $info['password']) {
            die('Username or Password is incorrect! Please <a href="index.php">try again</a>.');
        }
        else { 
            // if login is ok then we add a cookie 
            $_POST['username'] = stripslashes($_POST['username']); 
            $hour = time() + 3600; 
            setcookie(dfengineid, $_POST['username'], $hour); 
            setcookie(dfenginekey, $_POST['pass'], $hour);   
            //then redirect them to the members area 
            header("Location: members.php"); 
        } 
    } 
} 
else {   
 // if they are not logged in 
 ?> 

 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
 <table border="0"> 
 <tr><td>Username:</td><td> 
 <input type="text" name="username" maxlength="40"> 
 </td></tr> 
 <tr><td>Password:</td><td> 
 <input type="password" name="pass" maxlength="50"> 
 </td></tr>    
 <tr><td colspan="2" align="right"> 
 <input type="submit" name="submit" value="Login"> 
 </td></tr> 
 </table> 
 </form> 

 <?php } ?> 

注意:问题已解决我将代码放在错误的位置,因此出现了所有这些错误。

5 个答案:

答案 0 :(得分:4)

setcookie(dfengineid, $_POST['username'], $hour);
setcookie(dfenginekey, $_POST['pass'], $hour);

应该是

setcookie('dfengineid', $_POST['username'], $hour);
setcookie('dfenginekey', $_POST['pass'], $hour);

答案 1 :(得分:3)

setcookie(dfengineid, $_POST['username'], $hour); 

setcookie(dfenginekey, $_POST['pass'], $hour);  

你在这里使用dfenginekey作为常量,我认为你的意思是这样做:

setcookie('dfengineid', $_POST['username'], $hour); 

setcookie('dfenginekey', $_POST['pass'], $hour);  

我编辑了你的代码,现在应该工作了!

<?php
// Connects to your Database 

mysql_connect("localhost", "root", "---") or die(mysql_error());
mysql_select_db("dfeuserdb") or die(mysql_error());

//This code runs if the form has been submitted
//Checks if there is a login cookie
if (isset($_COOKIE['dfengineid']))
//if there is, it logs you in and directes you to the members page
{
    $username = $_COOKIE['dfengineid'];
    $pass = $_COOKIE['dfenginekey'];
    $check = mysql_query("SELECT * FROM users WHERE username = '". mysql_real_escape_string($username)."'") or die(mysql_error());

    while ($info = mysql_fetch_array($check))
    {
        if ($pass != $info['password'])
        {

        }
        else
        {
            die('<META HTTP-EQUIV=Refresh CONTENT="0; URL=search.php">');
        }
    }
}

//if the login form is submitted     
if (isset($_POST['submit']))
{ // if form has been submitted
    // makes sure they filled it in  
    if (!$_POST['username'] | !$_POST['pass'])
    {
        die('You did not fill in a required field.');
    }

    // checks it against the database
    $check = mysql_query("SELECT * FROM users WHERE username = '" . $_POST['username'] . "'") or die(mysql_error());

    //Gives error if user dosen't exist
    $check2 = mysql_num_rows($check);
    if ($check2 == 0)
    {
        die('Username or Password is incorrect! Please <a href="index.php">try again</a>.');
    }

    while ($info = mysql_fetch_array($check))
    {
        $_POST['pass'] = stripslashes($_POST['pass']);
        $info['password'] = stripslashes($info['password']);
        $_POST['pass'] = md5($_POST['pass']);

        //gives error if the password is wrong

        if ($_POST['pass'] != $info['password'])
        {
            die('Username or Password is incorrect! Please <a href="index.php">try again</a>.');
        }
        else
        {
            // if login is ok then we add a cookie 
            $_POST['username'] = stripslashes($_POST['username']);
            $hour = time() + 3600;
            setcookie('dfengineid', $_POST['username'], $hour);
            setcookie('dfenginekey', $_POST['pass'], $hour);
            //then redirect them to the members area 
            die( '<META HTTP-EQUIV=Refresh CONTENT="0; URL=members.php">');
        }
    }
}
else
{
    // if they are not logged in 
    ?> 

    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
        <table border="0"> 
            <tr><td>Username:</td><td> 
                    <input type="text" name="username" maxlength="40"> 
                </td></tr> 
            <tr><td>Password:</td><td> 
                    <input type="password" name="pass" maxlength="50"> 
                </td></tr>    
            <tr><td colspan="2" align="right"> 
                    <input type="submit" name="submit" value="Login"> 
                </td></tr> 
        </table> 
    </form> 

<?php } ?>

答案 2 :(得分:2)

更改以下行
setcookie(dfengineid, $_POST['username'], $hour);

setcookie(dfenginekey, $_POST['pass'], $hour); 

setcookie('dfengineid', $_POST['username'], $hour);

setcookie('dfenginekey', $_POST['pass'], $hour); 

答案 3 :(得分:2)

第一

setcookie('dfengineid', $_POST['username'], $hour); 

而不是

setcookie(dfengineid, $_POST['username'], $hour); 

seccond: 由于您在header之前输出了无法重定向的信息,因此可以解决错误或使用ob_start()

答案 4 :(得分:2)

 setcookie('dfengineid', $_POST['username'], $hour); 
 setcookie('dfenginekey', $_POST['pass'], $hour);  

在没有qoutes的情况下使用此代码会给您带来错误 由于输出此错误而导致的下一个错误

相关问题