用php做一个简单的注册用户功能

首先在register.php页面可以做一个静态的注册页面,代码如下

<?php

<form action="do_register.php" method="post">

用户名:<input type='text' name="user" id="1"><br>

密码:<input type='password' name="pwd" id="2"><br>

确认密码:<input type='password' name="confirmpwd" id="3"><br>

<input type='submit' name="sub" value="sub">

</form>

然后再在do_register.php页面控制注册,代码如下

<?php

  if(!$_POSTt['sub']){

    exit("请点击注册按钮");

  }

  $_POST['user']=trim(strip_tags($_POST['user'])));

  if(!$_POST['user']){

    exit("用户名不能为空");

  }

  $_POST['pwd']=trim(strip_tags($_POST['pwd'])));

 if(!$_POST['pwd']){

    exit("密码不能为空");

  }

  $_POST['confirmpwd']=trim(strip_tags($_POST['confirmpwd'])));

  if(!$_POST['confirmpwd']){

    exit("确认密码不能为空");

  }

  if($_POST['pwd']!=$_POST['confirmpwd']){

    exit("密码与确认密码不相等");

  }

  $_POST['pwd']=md5($_POST['pwd']);

  $arr=[];

  $arr['user']=$_POST['user'];

  $arr['pwd']=$_POST['pwd'];

  $file="userinfo.txt";

  if(is_file($file)){

  $files=file($file);

  foreach($files as $v){

    $v=substr($v,0,-2);

    $v=json_decode($v,true);

    foreach($v as $v1){

    if($_POST['user']==$v1['user']){

        exit("用户名重名,请重新输入");

      }

    }

  }

}

$arr=json_decode($arr);

$con=file_put_contents($file,$arr." ",FILE_APPEND);

$lenth=strlen($arr)+2;

if($con==$lenth){

  echo "注册成功";

}else{

  echo "注册失败";

}

?>

原文地址:https://www.cnblogs.com/gujunlin/p/7443721.html