1

几个月前我为我的项目建立了一个注册功能,直到昨天它一直运行良好,我不太确定发生了什么,因为我已经好几个星期没有用它改变任何东西了,当时我上次检查时它正在工作。我试图自己调试它,但我找不到任何问题,我不知道还能做什么!

代码分为多个页面,但本质上,这是发生了什么:

HTML 构造

<?php require_once("clean.php"); ?>

<ul class="nav pull-right"><?php
                        if (isset($_SESSION['logged'])) {?>
                            <li><a href="profile.php">Profile</a></li>
                            <li><a href="logout.php">Logout</a></li><?php
                        } else {?>
                            <li><a href="#register" class="account-register" data-toggle="modal" title="Register a new Screening account">Register</a></li>
                            <li><a href="#login" class="account-login" data-toggle="modal" title="Login to your Screening profile">Login</a></li><?php
                        }?>
                    </ul>

<div id="register" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="registerLabel" aria-hidden="true">
        <?php require_once("register-controller.php"); ?>

        <!-- reCAPTCHA jQuery -->
        <script type="text/javascript">
             var RecaptchaOptions = {
                theme : 'white'
             };
         </script>

        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="registerLabel" class="modal-title">Register a new Screening account</h3>
        </div>

        <form name="register" action="" method='POST' enctype="multipart/form-data">
            <div class="modal-body">

                <?php echo $register_bad_message; ?>
                <?php echo $register_good_message; ?>


                <input class="input-block-level" type="text" name="firstname" placeholder="First Name">
                <input class="input-block-level" type="text" name="lastname" placeholder="Last Name">
                <input class="input-block-level" type="email" name="email" placeholder="Email">
                <input type="file" class="profile-picture-upload" name="profile-image" alt="profile-image">
                <input class="input-block-level" type="password" name="password" placeholder="Password">
                <input class="input-block-level" type="password" name="confirm-password" class="span3" placeholder="Confirm Password">
                <?php include ("recaptcha_form.php") ?>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
                <button type="submit" class="btn btn-success" name="submit" value="Sign up!">Sign up!</button>
            </div>
        </form>
    </div>

清洁.php

<?php
/*
        ini_set('display_errors', 1);
    error_reporting(E_ALL);
*/
    function clean_string($string) {
        $string = trim($string);
        $string = utf8_decode($string);
        $string = str_replace("#", "&#35", $string); $string = str_replace("%", "&#37", $string);

        if (mysql_real_escape_string($string)) {
            $string = mysql_real_escape_string($string);
        }

        if (get_magic_quotes_gpc()) {
            $string = stripslashes($string);
        }

        return htmlentities($string);
    }
?>

注册控制器.php

<?php
    /*
ini_set('display_errors', 1);
    error_reporting(E_ALL);
*/
    class SimpleImage {

       var $image;
       var $image_type;

       function load($filename) {

          $image_info = getimagesize($filename);
          $this->image_type = $image_info[2];
          if( $this->image_type == IMAGETYPE_JPEG ) {

             $this->image = imagecreatefromjpeg($filename);
          } elseif( $this->image_type == IMAGETYPE_PNG ) {

             $this->image = imagecreatefrompng($filename);
          }
       }
       function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image,$filename,$compression);
          } elseif( $image_type == IMAGETYPE_PNG ) {

             imagepng($this->image,$filename);
          }
          if( $permissions != null) {

             chmod($filename,$permissions);
          }
       }
       function output($image_type=IMAGETYPE_JPEG) {

          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image);
          } elseif( $image_type == IMAGETYPE_PNG ) {

             imagepng($this->image);
          }
       }
       function getWidth() {

          return imagesx($this->image);
       }
       function getHeight() {

          return imagesy($this->image);
       }
       function resizeToHeight($height) {

          $ratio = $height / $this->getHeight();
          $width = $this->getWidth() * $ratio;
          $this->resize($width,$height);
       }

       function resizeToWidth($width) {
          $ratio = $width / $this->getWidth();
          $height = $this->getheight() * $ratio;
          $this->resize($width,$height);
       }

       function scale($scale) {
          $width = $this->getWidth() * $scale/100;
          $height = $this->getheight() * $scale/100;
          $this->resize($width,$height);
       }

       function resize($width,$height) {
            $new_image = imagecreatetruecolor($width, $height);
            if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
                $current_transparent = imagecolortransparent($this->image);
                if($current_transparent != -1) {
                    $transparent_color = imagecolorsforindex($this->image, $current_transparent);
                    $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                    imagefill($new_image, 0, 0, $current_transparent);
                    imagecolortransparent($new_image, $current_transparent);
                } elseif( $this->image_type == IMAGETYPE_PNG) {
                    imagealphablending($new_image, false);
                    $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
                    imagefill($new_image, 0, 0, $color);
                    imagesavealpha($new_image, true);
                }
            }
            imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
            $this->image = $new_image;  
        }   

    }

    //Clean
    echo "before clean";
    $submit = clean_string($_POST['submit']);
    echo "after clean";

    if ($submit == 'Sign up!') {


        $first_name = clean_string($_POST['first-name']);
        $last_name = clean_string($_POST['last-name']);
        $email = clean_string($_POST['email']);
        $password = clean_string($_POST['password']);
        $confirm_password = clean_string($_POST['confirm-password']);

        //Output variables
        $register_bad_message = '';
        $register_good_message = '';

        require_once($_SERVER['DOCUMENT_ROOT'] . '/recaptcha/recaptchalib.php');
        $privatekey = "6Ldbd8ASAAAAAFz8VT29H5w4WLNjsbI-mFY2QkaC";
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);
        if (!$resp->is_valid) {
            $errMessage = $resp->error;
            $register_bad_message = '<div class="alert alert-error">The reCAPTCHA you entered wasn\'t correct. Please try again.</div>';?>
            <script>
                $('a.account-register').trigger('click');
            </script><?php
        } else {
            if ($first_name&&$last_name&&$email&&$password&&$confirm_password) {
                if ($password == $confirm_password) {
                    if (strlen($password) > 25 || strlen($password) < 6) {
                        $register_bad_message = '<div class="alert alert-error">Please enter a password between 6 and 25 characters.</div>';?>
                        <script>
                            $('a.account-register').trigger('click');
                        </script><?php
                    } else {
                        require_once("db_connect.php");
                        if($db_server) {
                            $first_name = clean_string($first_name);
                            $last_name = clean_string($last_name);
                            $email = clean_string($email);
                            $password = clean_string($password);
                            echo "1";
                            mysql_select_db($db_database);

                            $taken = mysql_query("SELECT email FROM users WHERE email='$email'");
                            $count = mysql_num_rows($taken);
                            if ($count > 0) {
                                $register_bad_message = '<div class="alert alert-error">The email you have entered is already associated with a Screening account. Please choose another.</div>';?>
                                <script>
                                    $('a.account-register').trigger('click');
                                </script><?php
                            } else {
                                if ($_FILES) {
                                    //Put file properties into variables
                                    $file_name = $_FILES['profile-image']['name'];
                                    $file_size = $_FILES['profile-image']['size'];
                                    $file_tmp_name = $_FILES['profile-image']['tmp_name'];


                                    //Determine filetype
                                    switch ($_FILES['profile-image']['type']) {
                                        case 'image/jpeg': $ext = "jpg"; break;
                                        case 'image/png': $ext = "png"; break;
                                        default: $ext = ''; break;
                                    }

                                    if ($ext) {
                                        //Check filesize
                                        if ($file_size < 5242880) {
                                            //Process file - resize, clean up filename and move to safe location
                                            $image = new SimpleImage();
                                            $image->load($file_tmp_name);
                                            $image->resizeToWidth(250);
                                            $image->save($file_tmp_name);


                                            $n = "$file_name";
                                            $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
                                            $n = strtolower($n);
                                            $n = "avatars/$n";
                                            move_uploaded_file($file_tmp_name, $n);
                                        } else {
                                            $register_bad_message = '<div class="alert alert-error">Please ensure your chosen file is less than 5MB.</div>';?>
                                            <script>
                                                $('a.account-register').trigger('click');
                                            </script><?php
                                        }
                                    } else {
                                        $register_bad_message = '<div class="alert alert-error">Please ensure your image is of filetype .jpg or.png.</div>';?>
                                        <script>
                                            $('a.account-register').trigger('click');
                                        </script><?php
                                    }
                                }
                                $password = md5($password);
                                $query = "INSERT INTO users (first_name, last_name, email, password, image) VALUES ('$first_name', '$last_name', '$email', '$password', '$n')";
                                mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                                $register_good_message = '<div class="alert alert-success">Registration successful!
                                                            <br />
                                                            <a href='#login' data-toggle='modal' title='Login to your Screening profile'>Login now</a></div>';?>
                                <script>
                                    $('a.account-register').trigger('click');
                                </script><?php
                            }
                        } else {
                            $register_bad_message = '<div class="alert alert-error">Error: could not connect to the database.</div>';?>
                            <script>
                                $('a.account-register').trigger('click');
                            </script><?php
                        }
                        require_once("db_close.php");
                    }
                } else {
                    $register_bad_message = '<div class="alert alert-error">Passwords failed to match. Please try again.</div>';?>
                    <script>
                        $('a.account-register').trigger('click');
                    </script><?php
                }
            } else {
                $register_bad_message = '<div class="alert alert-error">Please fill in all fields before continuing.</div>';?>
                <script>
                    $('a.account-register').trigger('click');
                </script><?php
            }
        }
    }

?>

如果我打开错误报告,我收到的消息是:

清除前警告:mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php on第 11 行警告:mysql_real_escape_string(): A link could not be established in \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php on line 11 after clean 注意:未定义索引:第 100 行 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\register-controller.php 中的名字 警告:mysql_real_escape_string(): Access denied for user ''@'localhost '(使用密码:NO)在第 11 行的 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 警告:mysql_real_escape_string():无法在第 11 行的 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 中建立到服务器的链接 注意:未定义索引:\ICS-FILESHARE 中的姓氏\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\register-controller.php 第 101 行警告:mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in \ICS -FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 第 11 行警告:mysql_real_escape_string():无法在 \ICS-FILESHARE\WWW\newmedia 中建立到服务器的链接。 leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 在第 11 行警告:mysql_real_escape_string():在 \ICS-FILESHARE\WWW\newmedia 中用户 ''@'localhost'(使用密码:NO)的访问被拒绝。 leeds.ac.uk\ug10\cs10aer\screening_new\clean。第 11 行的 php 警告:mysql_real_escape_string():无法在 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 中建立到服务器的链接 11 警告:mysql_real_escape_string (): 第 11 行 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 中的用户 ''@'localhost' 的访问被拒绝(使用密码:NO)警告:mysql_real_escape_string ():无法在第 11 行的 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 中建立到服务器的链接 警告:mysql_real_escape_string(): Access denied for user ''@'localhost'(使用密码:NO)在 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 第 11 行警告:mysql_real_escape_string():无法在第 11 行的 \ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening_new\clean.php 中建立到服务器的链接

但是我检查了我的数据库连接,一切似乎都正常 - 网站上的其他功能,例如登录和添加、删除和修改数据库详细信息都运行良好,并且它们使用相同的数据库连接脚本,所以只是这个注册由于我无法理解的原因,突然损坏的功能。

4

2 回答 2

3

您必须在调用之前连接到数据库mysql_real_escape_string。当代码到达这一点时,连接尚未建立:

//Clean
echo "before clean";
$submit = clean_string($_POST['submit']);
echo "after clean";

或者,您可以使用 not-as-safemysql_escape_string功能。mysql_real_escape_string需要数据库连接,因为它需要知道连接的字符编码。

于 2013-03-24T11:57:16.763 回答
1

更好的是,不要使用 php mysql 函数和即将被弃用的 mysql_real_esape_string() 而是使用更新和更安全的 PDO。

于 2013-03-24T14:01:02.377 回答