PHP代码在本地但不在远程服务器上工作

时间:2015-04-20 05:28:44

标签: php

抱歉!我的错.. 此代码显示用户注册表单,对帖子进行验证并将信息保存到文件中。

<html>
    <head>
    <style>
    #main {
        width: auto;
        padding: 25px;
        border: 25px solid green;
        margin: 25px;
    }
    input[type=text]{
    display:inline-block;
    border: 1px solid #999;
    height: 25px;
    margin-bottom: 2em;
    padding: .75em .5em;
    }
    input[type=password]{
    display:inline-block;
    border: 1px solid #999;
    height: 25px;
    margin-bottom: 2em;
    padding: .75em .5em;
    }
    input[type=submit] {
        color:#08233e;
        font:bold 2.4em Futura, ‘Century Gothic’, AppleGothic, sans-serif;
        font-size:90%;
        cursor:pointer;
        border: 2px solid blue;
    }
    .error {
        color: #FF0000;
        font: italic bold 15px arial;
    }   
    </style>
    </head>
    <body>
    <div id="main">
    <?php
    require_once 'Mail.php';
    //dns101.comcast.net, Smtp.comcast.net
    $fname = $lname = $uname = $pwd = $cpwd = $email = "";
    $firsterr = $lasterr = $unameerr = $pwderr = $pwdmatch = $emailerr = "";
    $isError=false;
    if (isset($_POST['submit'])) {
            validateInput();
        if ($isError) {
            display_form();
        }
        else {
            process_form();
        }
        }
        else {
            display_form();// display form for the first time
        }

    function display_form() {
        global $firsterr, $lasterr, $unameerr, $pwderr, $pwdmatch, $emailerr ;
        echo "<h1>Registration</h1>";
        echo "<form action = $_SERVER[SCRIPT_NAME] method=post>";
        echo "<span class='error'>  $pwdmatch </span><br><br>";
        $value=isset($_POST['fname'])?$_POST['fname']:'';
        echo "First Name:<input type='text' name='fname' size='50' maxlength='80' value=\"$value\" >";
        echo "<span class='error'>  $firsterr </span><br>";

        $value=isset($_POST['lname'])?$_POST['lname']:'';
        echo "Last Name:<input type='text' name='lname' size='50' maxlength='80' value=\"$value\" >";
        echo "<span class='error'>  $lasterr </span><br>";

        $value=isset($_POST['email'])?$_POST['email']:'';
        echo "Email:<input type='text' name='email' size='50' maxlength='80' value=\"$value\" >";
        echo "<span class='error'>  $emailerr </span><br>";

        $value=isset($_POST['uname'])?$_POST['uname']:'';
        echo "User Name:<input type='text' name='uname' size='50' maxlength='80' value=\"$value\" >";
        echo "<span class='error'>  $unameerr </span><br>";

        echo "Password: <input type='password' name='pwd' size='50' maxlength='80' >";
        echo "<span class='error'>  $pwderr </span><br>";
        echo <<<HTML

        Confirm Password:   <input type="password" name="cpwd" size="50" maxlength="80" ><br>
        <input type="submit"  name="submit" value="Submit">
        </form>
    HTML;
    }
    function cleanData($data) {
        $data = stripslashes(trim($data));
        $data = htmlspecialchars($data);
        return $data;
    }
    function validateInput(){
    global $firsterr, $lasterr, $unameerr, $pwderr, $pwdmatch, $emailerr, $isError;
        //check if fname, lname and user name are empty
        if(empty(cleanData($_POST['fname']))){
            $firsterr="* first name is required";
            $isError=true;
        }
        if(empty(cleanData($_POST['lname']))){
            $lasterr=" * last name is required";
            $isError=true;
        }
        if(empty(cleanData($_POST['uname']))){
            $unameerr="* user name is required";
            $isError=true;
        }

        // check email format
        $pattern = '/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/';

        if(preg_match($pattern,cleanData($_POST['email']))==0){
            $emailerr="* a valid email-address is required.";
            $isError=true;
        }

        if(strcmp($_POST['pwd'],$_POST['cpwd'])){
            $pwdmatch="*** Your password did not match your confirmed password ***";    
            $isError=true;
        }

        if(empty($_POST['pwd'])){
            $pwderr="* password cannot be empty";
            $isError=true;
        }
    }


    function process_form() {
        $data_dir = "data";
        // the users.txt file stores the users' information
        $file = "$data_dir/users.txt";
        if ($fh = fopen ($file, 'a+bt')) {
                // create directory for user based on time the user registered and a random value.
                // This guarantees that the directory is unique and has a valid name.
                $dir = time().rand(0, 4596);
                // create the data to be written (on Windows add \r\n)
                // use the crypt() to encrypt the password.
                $data = $_POST['fname']."|".$_POST['lname']."|".$_POST['uname']."|".$_POST['email']."|".$_POST['pwd']."|".crypt($_POST['pwd'],$dir)."|".$dir."\r\n";
                // write the data and close the file
                fwrite ($fh, $data);
                // close the directory in the data directory
                mkdir ("$data_dir/$dir");
                // print a message
                echo "Thank You $_POST[fname] $_POST[lname] for registering. <br>";
                // loop through the file by reading 1000 bytes or one line
                // whichever comes first with each iteration.
                // The data being read is broken into an array using | as delimiter.
                rewind($fh);
                while ($line = fgetcsv($fh, 1000, "|")) {
                    // check the file data against the submitted data
                if (($line[2] == $_POST['uname']) && ($line[5] == crypt($_POST['pwd'], $line[6]))) {
                    echo "<p>Here is your information: </p>";
                    echo $line[0]."|".$line[1]."|".$line[2]."|".$line[3]."|".$line[4];
                    // stop looking through the file
                    break;
                }
            }
                fclose ($fh);   
            }
            else {
                // couldn't write to the file
                echo "<p>You couldn't be registered due to a system error.</p>";
            }

        $message = "
        <html>
        <head>
        <title>Sending an HTML Message</title>
        </head>
        <body>
        <h2>Thank you for registering</h2>
        <h2 align=center>Your user name is {$_POST['uname']}<br><br>Your password is: {$_POST['pwd']}</h2>
        </body>
        </html> ";
        $msgHeader = "From: infoFromRuchi@itu.com\r\n";
        $msgHeader .= "MIME-Version: 1.0\n";
        $msgHeader .= "Content-type: text/html; charset=us-ascii\n";

        // Send email
        $test= mail($_POST['email'], 'Registration Confirmation', $message, $msgHeader);
        //echo $test;
        echo "<p>You will receive an email confirming your registration.</p>";
    }

    ?>

    </div>
    </body>
    </html>

如果成功,它会从文件中读回信息并向用户发送确认电子邮件。

这在我的本地运行(除了电子邮件部分)但是当我ftp到域服务器时,它呈现一个空白页面。它正在显示其他页面,因此我的代码中必须存在一些问题。

1 个答案:

答案 0 :(得分:0)

我找到了答案。

空函数只接受变量,不能像:

一样使用

空(cleanData($ _ POST [ 'FNAME']))

使用(strlen(cleanData($ _ POST ['fname']))== 0)解决了这个问题。