php文件在localhost上完美运行,但在webserver

时间:2017-01-18 15:07:43

标签: php webserver localhost

大家都过得愉快,我已经在一段时间内与一个问题作斗争了。我有一个完美的本地主机PHP程序,但其中一个PHP文件拒绝在网络服务器.ie上工作。上传后在我的网站上。所有其他php文件都有效,但register.php文件拒绝在Web服务器上运行。可能是什么原因。我做错了什么。请帮助。

 // register.php code





<?php
        include('header.php'); 
        include('navbar_teacher.php');
        include 'core/init.php';




        logged_in_redirect();


        if (empty($_POST) === false) {

            $required_fields = array('username ','password','password_again','first_name','class','mac','phone_no','email','js1','js2','address');
            foreach ($_POST as $key=>$value) {
                if (empty($value) && in_array($key, $required_fields) ===  true) { 
                $errors[] = 'Fields marked with an asterisk are required';
                break 1;
                }
                }
            if (empty($errors) === true) {
            if (user_exists($_POST['username']) === true) {
                $errors[] = 'Sorry, the username  \''. $_POST['username'].'\' is already taken.';
            }
            if (preg_match("/\\s/", $_POST['username']) === true) {
                $errors[] = 'Your username must not contain any space.';
            }
            if (strlen($_POST['password']) < 6 ) {
                $errors[] = 'Your password must be at least 6 characters.';
            }
            if ($_POST['password'] !== $_POST['password_again']) {
                $errors[] = 'Your password do not match.';
        }
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)=== false) {
            $errors[] = 'A valid email address is required';
        }
        if (email_exists($_POST['email']) === true) {
            $errors[] = 'Sorry, the email  \''. $_POST['email'].'\' is already in use.';
        }
            }
        }
        ?> 

        <?php
        if (isset($_GET ['success'])=== true && empty($_GET['success'])=== true) {
            echo 'You\'ve been registered successfully! Please check your email to activate your account.';
        }else {
        if (empty($_POST) === false && empty($errors) === true) {
            $register_data =array(
            'username'       => $_POST['username'],
            'password'       => $_POST['password'],
            'first_name'       => $_POST['first_name'],
            'last_name'        => $_POST['last_name'],
            'class'             => $_POST['class'],
            'mac'               => $_POST['mac'],
            'phone_no'              => $_POST['phone_no'],
            'email'            => $_POST['email'],

            'address'          => $_POST['address'],
            'email_code'       =>md5($_POST['username'] + microtime())  
        );
        register_user($register_data);
        //header('Location: register.php?success');
        echo("<script>location.href = 'register.php?success=$msg';</script>");
        exit();
        } else if (empty($errors) === false)  {
            echo output_errors($errors);
        }
        ?>
        <body id="class_div">



        <div class="span8" id="content">
                             <div class="row-fluid">
        <?php include('about.php'); ?>

        </div>
                </div>


         <div class="row-fluid">
         <div class="span4">
            <?php include 'add_register.php';?>                    <!-- block -->




        </body>
        </html>
        <?php 
        }

        include 'includes/overall/footer.php';?> 

// init.php代码

 <?php
    session_start();
    //error_reporting(0);

    require 'database/connect.php';
    require 'functions/general.php';
    require 'functions/users.php';

    $current_file = explode('/', $_SERVER['SCRIPT_NAME']);
    $current_file = end($current_file);



    if(logged_in() === true) {
    $session_user_id = $_SESSION['user_id'];
    $user_data = user_data($session_user_id, 'user_id','username','password','first_name', 'last_name','class','mac','phone_no','email','address','email_code','active','password_recover','type','allow_email','profile');
    if (user_active($user_data['username']) === false) {
        session_destroy();
        //header('Location: index.php');
        echo("<script>location.href = 'index.php=$msg';</script>");
        exit();
    }
    if ($current_file !== 'changepassword.php' && $user_data['password_recover']== 1) { 
        header('Location:  changepassword.php?force');
        exit();
    }
    } 
    //echo  $user_data['type'];
    $errors = array();

    ?>






    //users.php code



<?php

   function register_user($register_data) {
    array_walk($register_data, 'array_sanitize');

    $register_data['password'] =md5($register_data['password']);
    $fields =  '`' . implode('`, `', array_keys($register_data)) . '`';
    $data   =  '\'' . implode('\', \'', $register_data) . '\''; 

    $db->query(("INSERT INTO `users` ($fields) VALUES ($data)");
    email($register_data['email'], 'Activate your account', "Hello "  .  $register_data ['first_name'] . ",\n\n You need to activate your account, so use the link below:\n\nhttp://ckischools.org/personal/activate.php?email=" . $register_data['email'] . "&email_code=" .$register_data['email_code'] . "\n\n -ckischools ");

    }

所有其他php文件在服务器上运行良好,除了register.php,其中包含与init.php和users.php的链接

2 个答案:

答案 0 :(得分:0)

不确定是否是原因 - 但我会将包含顺序更改为:

include('core/init.php'); 
include('navbar_teacher.php');
include('header.php');

因为您在core/init.php中启动了会话,必须在任何输出之前启动

http://php.net/manual/en/function.session-start.php#refsect1-function.session-start-notes

您可以通过测试会话是否实际创建来添加进一步检查:

$sessionStarted = session_start();

if($sessionStarted)
{
    // code here
}
else
{
    // echo('Could not start session!');
}

答案 1 :(得分:0)

我最终找到了问题的解决方案,感谢您的所有贡献。我会在问题所在的地方找到解决方案,并且我能获得完美的解决方案。 问题出在登记表和提交表上

//初始表格     

                                <div class="control-group">
                                <div class="controls">
                                 <ul>
<li>
<div class="control-group">
<label>UserName*:</label>
<input  type="text" name="username">
</div>
</li>
<li>
<div class="control-group">
<label>Password*:</label>:Hint-  Minimum 6 characters<br>
<input  type="password" name="password">
</div>
</li>
<li>
<div class="control-group">
<label>Password Again*:</label>
<input  type="password" name="password_again">
</div>
</li>
<li>
<div class="control-group">
<label>FirstName*:</label>
<input  type="text" name="first_name">
</div>
</li>
<li>
<div class="control-group">
<label>LastName:</label>
<input  type="text" name="last_name" />
</div>
</li>
<li>
<div class="control-group">
<label>Student's Class:</label>
<input  type="text" name="class" />
</div>

</li>
<li>
<div class="control-group">

<label>Unique Address:</label>
<input  type="text" name="mac" />
</div>

</li>
<li>
<div class="control-group">

<label>Phone Number:</label>
<input  type="text" name="phone_no" />
</div>
</li>
<li>
<div class="control-group">
<label>Email*:</label>
<input  type="text" name="email">
</div>
</li>

<li>
<div class="control-group">
<label>Contact Address:</label>
<textarea name="address"></textarea>
</div>
</li>
<input  type="submit" value="Register">
</li>
</ul>
</div>
                                        </div>
</form>

//初始提交表单

<?php
if (isset($_GET ['success'])=== true && empty($_GET['success'])=== true) {
    echo 'You\'ve been registered successfully! Please check your email to activate your account.';
}else {
if (empty($_POST) === false && empty($errors) === true) {
    $register_data =array(
    'username'       => $_POST['username'],
    'password'       => $_POST['password'],
    'first_name'       => $_POST['first_name'],
    'last_name'        => $_POST['last_name'],
    'class'             => $_POST['class'],
    'mac'               => $_POST['mac'],
    'phone_no'              => $_POST['phone_no'],
    'email'            => $_POST['email'],

    'address'          => $_POST['address'],
    'email_code'       =>md5($_POST['username'] + microtime())  
);
register_user($register_data);
//header('Location: register.php?success');
echo("<script>location.href = 'register.php?success=$msg';</script>");
exit();
} else if (empty($errors) === false)  {
    echo output_errors($errors);
}
?>

问题是通过在表单中​​添加'email_code'来解决的,因为在我的表格中,email_code未定义为0或1。 'email_code'=&gt; md5($ _ POST ['用户名'] + microtime())

solution
 <li>
    <div class="control-group">
    <label>RE-Type Email*:</label>
    <input  type="text" name="email_code">
    </div>
    </li>

//初始表格     

                                <div class="control-group">
                                <div class="controls">
                                 <ul>
<li>
<div class="control-group">
<label>UserName*:</label>
<input  type="text" name="username">
</div>
</li>
<li>
<div class="control-group">
<label>Password*:</label>:Hint-  Minimum 6 characters<br>
<input  type="password" name="password">
</div>
</li>
<li>
<div class="control-group">
<label>Password Again*:</label>
<input  type="password" name="password_again">
</div>
</li>
<li>
<div class="control-group">
<label>FirstName*:</label>
<input  type="text" name="first_name">
</div>
</li>
<li>
<div class="control-group">
<label>LastName:</label>
<input  type="text" name="last_name" />
</div>
</li>
<li>
<div class="control-group">
<label>Student's Class:</label>
<input  type="text" name="class" />
</div>

</li>
<li>
<div class="control-group">

<label>Unique Address:</label>
<input  type="text" name="mac" />
</div>

</li>
<li>
<div class="control-group">

<label>Phone Number:</label>
<input  type="text" name="phone_no" />
</div>
</li>
<li>
<div class="control-group">
<label>Email*:</label>
<input  type="text" name="email">
</div>
</li>
<li>
<div class="control-group">
<label>RE-Type Email*:</label>
<input  type="text" name="email_code">
</div>
</li>
<li>
<div class="control-group">
<label>Contact Address:</label>
<textarea name="address"></textarea>
</div>
</li>
<input  type="submit" value="Register">
</li>
</ul>
</div>
                                        </div>
</form>