注册表单代码无效

时间:2017-01-27 13:53:26

标签: php ajax

Hello Guys这是注册表格。请阅读此代码并尝试回答 我对此代码有两个问题

1)我收到警告

  

警告:无法修改标题信息 - 第295行C:\ xampp \ htdocs \ signup.php中已经发送的标题(输出从C:\ xampp \ htdocs \ signup.php:252开始)

2)当我在填写表格后点击创建帐户时,按钮什么都不做 即它不会在表中插入值。

<?php
    session_start();
    // If user is logged in, header them away
    if(isset($_SESSION["username"])){
        header("location: message.php?msg=NO to that weenis");
        exit();
    }
    ?><?php
    // Ajax calls this NAME CHECK code to execute
    if(isset($_POST["usernamecheck"])){
        include_once("includes/db_connnection.php");
        $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
        $sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
        $query = mysqli_query($db_conx, $sql); 
        $uname_check = mysqli_num_rows($query);
        if (strlen($username) < 3 || strlen($username) > 16) {
            echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
            exit();
        }
        if (is_numeric($username[0])) {
            echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
            exit();
        }
        if ($uname_check < 1) {
            echo '<strong style="color:#009900;">' . $username . ' is OK</strong>';
            exit();
        } else {
            echo '<strong style="color:#F00;">' . $username . ' is taken</strong>';
            exit();
        }
    }
    ?><?php
    // Ajax calls this REGISTRATION code to execute
    if(isset($_POST["u"])){
        // CONNECT TO THE DATABASE
        include_once("includes/db_connection.php");
        // GATHER THE POSTED DATA INTO LOCAL VARIABLES
        $n = preg_replace('#[^a-z ]#i', '', $_POST['n']);
        $u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
        $e = mysqli_real_escape_string($db_conx, $_POST['e']);
        $p = $_POST['p'];
        $g = preg_replace('#[^a-z]#', '', $_POST['g']);
        $s = preg_replace('#[^a-z ]#i', '', $_POST['s']);
        // GET USER IP ADDRESS
        $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
        // DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
        $sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
        $query = mysqli_query($db_conx, $sql); 
        $u_check = mysqli_num_rows($query);
        // -------------------------------------------
        $sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
        $query = mysqli_query($db_conx, $sql); 
        $e_check = mysqli_num_rows($query);
        // FORM DATA ERROR HANDLING
        if($n == "" || $u == "" || $e == "" || $n == "" || $p == "" || $g == "" || $s == ""){
            echo "The form submission is missing values.";
            exit();
        } else if ($u_check > 0){ 
            echo "The username you entered is alreay taken";
            exit();
        } else if ($e_check > 0){ 
            echo "That email address is already in use in the system";
            exit();
        } else if (strlen($u) < 3 || strlen($u) > 16) {
            echo "Username must be between 3 and 16 characters";
            exit(); 
        } else if (is_numeric($u[0])) {
            echo 'Username cannot begin with a number';
            exit();
        } else {
        // END FORM DATA ERROR HANDLING
            // Begin Insertion of data into the database
            // Hash the password and apply your own mysterious unique salt
            $p_hash = md5($p);
            //$cryptpass = hash('sha1',$p);
            // Add user info into the database table for the main site table
            $sql = "INSERT INTO users (name, username, email, password, gender, stream, ip, signup, lastlogin, notescheck)       
                    VALUES('$n','$u','$e', '$p_hash','$g','$s','$ip',now(),now(),now())";
            $query = mysqli_query($db_conx, $sql); 
            $uid = mysqli_insert_id($db_conx);
            // Establish their row in the useroptions table
            $sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
            $query = mysqli_query($db_conx, $sql);
            // Create directory(folder) to hold each user's files(pics, MP3s, etc.)
            if (!file_exists("user/$u")) {
                mkdir("user/$u", 0755);
            }
            // Email the user their activation link
            $to = "$e";                          
            $from = "example@gmail.com";
            $subject = 'yoursitename Account Activation';
            $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>yoursitename Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.yoursitename.com"><img src="http://www.yoursitename.com/images/logo.png" width="36" height="30" alt="yoursitename" style="border:none; float:left;"></a>yoursitename Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.yoursitename.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
            $headers = "From: $from\n";
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\n";
            mail($to, $subject, $message, $headers);
            echo "signup_success";
            exit();
        }
        exit();
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Sign Up</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    #signupform{
        margin-top:24px;    
    }
    #signupform > div {
        margin-top: 12px;   
    }
    #signupform > input,select {
        width: 200px;
        padding: 3px;
        background: #F3F9DD;
    }
    #signupbtn {
        font-size:18px;
        padding: 12px;
    }
    #terms {
        border:#CCC 1px solid;
        background: #F5F5F5;
        padding: 12px;
    }
    </style>
    <script src="js/main.js"></script>
    <script src="js/ajax.js"></script>
    <script>
    function restrict(elem){
        var tf = _(elem);
        var rx = new RegExp;
        if(elem == "email"){
            rx = /[' "]/gi;
        } else if(elem == "username"){
            rx = /[^a-z0-9]/gi;
        }
        tf.value = tf.value.replace(rx, "");
    }
    function emptyElement(x){
        _(x).innerHTML = "";
    }
    function checkusername(){
        var u = _("username").value;
        if(u != ""){
            _("unamestatus").innerHTML = 'checking ...';
            var ajax = ajaxObj("POST", "signup.php");
            ajax.onreadystatechange = function() {
                if(ajaxReturn(ajax) == true) {
                    _("unamestatus").innerHTML = ajax.responseText;
                }
            }
            ajax.send("usernamecheck="+u);
        }
    }
    function signup(){
        var u = _("username").value;
        var e = _("email").value;
        var n = _("name").value;
        var p1 = _("pass1").value;
        var p2 = _("pass2").value;
        var s = _("stream").value;
        var g = _("gender").value;
        var status = _("status");
        if(n == "" || u == "" || e == "" || n == "" || p1 == "" || p2 == "" || s == "" || g == ""){
            status.innerHTML = "Fill out all of the form data";
        } else if(p1 != p2){
            status.innerHTML = "Your password fields do not match";
        } else if( _("terms").style.display == "none"){
            status.innerHTML = "Please view the terms of use";
        } else {
            _("signupbtn").style.display = "none";
            status.innerHTML = 'please wait ...';
            var ajax = ajaxObj("POST", "signup.php");
            ajax.onreadystatechange = function() {
                if(ajaxReturn(ajax) == true) {
                    if(ajax.responseText != "signup_success"){
                        status.innerHTML = ajax.responseText;
                        _("signupbtn").style.display = "block";
                    } else {
                        window.scrollTo(0,0);
                        _("signupform").innerHTML = "OK "+u+", check your email inbox and junk mail box at <u>"+e+"</u> in a moment to complete the sign up process by activating your account. You will not be able to do anything on the site until you successfully activate your account.";
                    }
                }
            }
            ajax.send("u="+u+"&n="+n+"&e="+e+"&n="+n+"&p="+p1+"&s="+s+"&g="+g);
        }
    }
    function openTerms(){
        _("terms").style.display = "block";
        emptyElement("status");
    }
    /* function addEvents(){
        _("elemID").addEventListener("click", func, false);
    }
    window.onload = addEvents; */
    </script>
    </head>
    <body>
    <div id="pageMiddle">
      <h3>Sign Up Here</h3>
      <form name="signupform" id="signupform" onsubmit="return false;">

        <div>Name:</div>
        <input id="name" type="text" onfocus="emptyElement('status')">
        <div>Username: </div>
        <input id="username" type="text" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16">
        <span id="unamestatus"></span>
        <div>Email Address:</div>
        <input id="email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('email')" maxlength="88">
        <div>Create Password:</div>
        <input id="pass1" type="password" onfocus="emptyElement('status')" maxlength="88">
        <div>Confirm Password:</div>
        <input id="pass2" type="password" onfocus="emptyElement('status')" maxlength="100">
        <div>Gender:</div>
        <select id="gender" onfocus="emptyElement('status')">
          <option value=""></option>
          <option value="m">Male</option>
          <option value="f">Female</option>
        </select>
        <div>Stream:</div>
        <select id="stream" onfocus="emptyElement('status')">
            <option disabled selected value>--select your option--</option>
    <option value="Arts">India</option>
    <option value="Biology">USA</option>
    <option value="Maths">Russia</option>

        </select>
        <div>
          <a href="#" onclick="return false" onmousedown="openTerms()">
            View the Terms Of Use
          </a>
        </div>
        <div id="terms" style="display:none;">
          <h3>Web Intersect Terms Of Use</h3>
          <p>1. Play nice here.</p>
          <p>2. Take a bath before you visit.</p>
          <p>3. Brush your teeth before bed.</p>
        </div>
        <br /><br />
        <button id="signupbtn" onclick="signup()">Create Account</button>
        <span id="status"></span>
      </form>
    </div>
    </body>
    </html>
    <?php
    if (isset($_GET['id']) && isset($_GET['u']) && isset($_GET['e']) && isset($_GET['p'])) {
        // Connect to database and sanitize incoming $_GET variables
        include_once("includes/db_connection.php");
        $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
        $u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
        $e = mysqli_real_escape_string($db_conx, $_GET['e']);
        $p = mysqli_real_escape_string($db_conx, $_GET['p']);
        // Evaluate the lengths of the incoming $_GET variable
        if($id == "" || strlen($u) < 3 || strlen($e) < 5 || strlen($p) != 74){
            // Log this issue into a text file and email details to yourself
            header("location: message.php?msg=activation_string_length_issues");
            exit(); 
        }
        // Check their credentials against the database
        $sql = "SELECT * FROM users WHERE id='$id' AND username='$u' AND email='$e' AND password='$p' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        // Evaluate for a match in the system (0 = no match, 1 = match)
        if($numrows == 0){
            // Log this potential hack attempt to text file and email details to yourself
            header("location: message.php?msg=Your credentials are not matching anything in our system");
            exit();
        }
        // Match was found, you can activate them
        $sql = "UPDATE users SET activated='1' WHERE id='$id' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        // Optional double check to see if activated in fact now = 1
        $sql = "SELECT * FROM users WHERE id='$id' AND activated='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        // Evaluate the double check
        if($numrows == 0){
            // Log this issue of no switch of activation field to 1
            header("location: message.php?msg=activation_failure");
            exit();
        } else if($numrows == 1) {
            // Great everything went fine with activation!
            header("location: message.php?msg=activation_success");
            exit();
        }
    } else {
        // Log this issue of missing initial $_GET variables
        header("location: message.php?msg=missing_GET_variables");
        exit(); 
    }
    ?>

ajax.js文件

function ajaxObj( meth, url ) {
    var x = new XMLHttpRequest();
    x.open( meth, url, true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}

main.js文件

function _(x){
    return document.getElementById(x);
}

    function toggleElement(x){
        var x = _(x);
        if(x.style.display == 'block'){
            x.style.display = 'none';
        }else{
            x.style.display = 'block';
        }
    }

0 个答案:

没有答案