未定义的变量和警告会话无法发送会话缓存限制器

时间:2014-02-28 14:03:09

标签: php mysql

嗨盖伊抱歉这个新手问题 因为我是php编程的新手.. 这是我的问题.. 我按照这个tutorials但我有很多错误.. 所有错误都是未定义的 我不知道如何修理它.. 任何建议或建议.. 提前谢谢......

   <?php  
include_once('config.php');  

// Reset errors and success messages  
$errors = array();  
$success = array();  

// Login attempt  
if(isset($_POST['loginSubmit']) && $_POST['loginSubmit'] == 'true'){  
    $loginEmail = trim($_POST['email']);  
    $loginPassword  = trim($_POST['password']);  

    if (!eregi("^[_a-z0-9-] (.[_a-z0-9-] )*@[a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3})$", $loginEmail))  
        $errors['loginEmail'] = 'Your email address is invalid.';  

    if(strlen($loginPassword) < 6 || strlen($loginPassword) > 12)  
        $errors['loginPassword'] = 'Your password must be between 6-12 characters.';  

    if(!$errors){  
        $query  = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($loginEmail) . '" AND password = MD5("' . $loginPassword . '") LIMIT 1';  
        $result = mysql_query($query);  
        if(mysql_num_rows($result) == 1){  
            $user = mysql_fetch_assoc($result);  
            $query = 'UPDATE users SET session_id = "' . session_id() . '" WHERE id = ' . $user['id'] . ' LIMIT 1';  
            mysql_query($query);  
            header('Location: index.php');  
            exit;  
        }else{  
            $errors['login'] = 'No user was found with the details provided.';  
        }  
    }  
}  

// Register attempt  
if(isset($_POST['registerSubmit']) && $_POST['registerSubmit'] == 'true'){  
    $registerEmail = trim($_POST['email']);  
    $registerPassword = trim($_POST['password']);  
    $registerConfirmPassword    = trim($_POST['confirmPassword']);  

    if (!eregi("^[_a-z0-9-] (.[_a-z0-9-] )*@[a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3})$", $registerEmail))   
        $errors['registerEmail'] = 'Your email address is invalid.';  

    if(strlen($registerPassword) < 6 || strlen($registerPassword) > 12)     
        $errors['registerPassword'] = 'Your password must be between 6-12 characters.';  

    if($registerPassword != $registerConfirmPassword)  
        $errors['registerConfirmPassword'] = 'Your passwords did not match.';  

    // Check to see if we have a user registered with this email address already  
    $query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($registerEmail) . '" LIMIT 1';  
    $result = mysql_query($query);  
    if(mysql_num_rows($result) == 1)   
        $errors['registerEmail'] = 'This email address already exists.';  

    if(!$errors){  
        $query = 'INSERT INTO users SET email = "' . mysql_real_escape_string($registerEmail) . '",  
                                                                        password = MD5("' . mysql_real_escape_string($registerPassword) . '"),  
                                                                        date_registered = "' . date('Y-m-d H:i:s') . '"';  

        if(mysql_query($query)){  
            $success['register'] = 'Thank you for registering. You can now log in on the left.';  
        }else{  
            $errors['register'] = 'There was a problem registering you. Please check your details and try again.';  
        }  
    }  

}  
?>  



<form class="box400" name="loginForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
    <h2>Login</h2>  
    <?php if($errors['login']) print '<div class="invalid">' . $errors['login'] . '</div>'; ?>  

    <label for="email">Email Address</label>  
    <input type="text" name="email" value="<?php echo htmlspecialchars($loginEmail); ?>" />  
    <?php if($errors['loginEmail']) print '<div class="invalid">' . $errors['loginEmail'] . '</div>'; ?>  

    <label for="password">Password <span class="info">6-12 chars</span></label>  
    <input type="password" name="password" value="" />  
    <?php if($errors['loginPassword']) print '<div class="invalid">' . $errors['loginPassword'] . '</div>'; ?>  

    <label for="loginSubmit">&nbsp;</label>  
    <input type="hidden" name="loginSubmit" id="loginSubmit" value="true" />  
    <input type="submit" value="Login" />  
</form>  

<form class="box400" name="registerForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
    <h2>Register</h2>  
    <?php if($success['register']) print '<div class="valid">' . $success['register'] . '</div>'; ?>  
    <?php if($errors['register']) print '<div class="invalid">' . $errors['register'] . '</div>'; ?>  

    <label for="email">Email Address</label>  
    <input type="text" name="email" value="<?php echo htmlspecialchars($registerEmail); ?>" />  
    <?php if($errors['registerEmail']) print '<div class="invalid">' . $errors['registerEmail'] . '</div>'; ?>  

    <label for="password">Password</label>  
    <input type="password" name="password" value="" />  
    <?php if($errors['registerPassword']) print '<div class="invalid">' . $errors['registerPassword'] . '</div>'; ?>  

    <label for="confirmPassword">Confirm Password</label>  
    <input type="password" name="confirmPassword" value="" />  
    <?php if($errors['registerConfirmPassword']) print '<div class="invalid">' . $errors['registerConfirmPassword'] . '</div>'; ?>  

    <label for="registerSubmit">&nbsp;</label>  
    <input type="hidden" name="registerSubmit" id="registerSubmit" value="true" />  
    <input type="submit" value="Register" />  
</form>  

1 个答案:

答案 0 :(得分:0)

如果您收到屏幕上的错误消息(例如“未定义的变量”),那么当然会将输出发送到浏览器。当第一个输出(标签或文本)被发送到浏览器时,累积的HTTP“标题”首先被刷新到浏览器。一旦发生这种情况,您就不能再为此页面发送更多标题。会话缓存限制器作为标头发送,因此存在无法发送的错误消息(由于文件被发送到浏览器,由于错误而导致标头已经被发送)。 / p>

简短形式:修复未定义的变量,会话缓存限制器错误将消失。