首页 > PHP
【复制】表单处理代码
来源:TP课堂 时间:2023-04-07 点击:448


use app\common\phpmailer\Email;
use think\Validate;
use think\exception\ValidateException;
 public function message_do(){

        $data=input('post.');
        $data['time']=time();
        $data['ip']=$this->getRealIP();
        $count=Db::name('message')->where('ip',$data['ip'])->whereDay('time')->count();
        if($count>5){
            return alert('You submitted it today','/',6,3);
        }
        try {
                    $this->validate( [
                        'first_name' =>$data['first_name'],
                        'last_name' => $data['last_name'],
                        'email' => $data['email'],
                        ], 'app\common\validate\Message');
                       
                } catch (ValidateException $e) {
                    echo "<div style='width:95vw;height:90vh;display:flex;align-items: center;
                    justify-content: center;'><div style='text-align:center'><img src='/static/index/images/error.png' style='width:80px'><h1 style='font-size:2em;'>".$e->getError()."</h1><br><a href='javascript:window.history.go(-1);' style='font-size:1em;colo:#1c6aba'>Back ></a></div></div>";
                    die();
                    //return alert($e->getError(),'/',6,3);
                }
        $res=Db::name('message')->insert($data);
        $email='1031950015@qq.com';//info@pioneerbelt.com
        $content="Name:".$data['name']."<br>Email:".$data['email']."<br>Phone:".$data['mobile']."<br>Sountry:".$data['subject']."<br>Company:".$data['company']."Content:".$data['content'];

        $e=new Email();
        $res=$e->send($email,$data['name'].'-Web Message',$content);
       
        return alert('Success','/',6);
    }
   
     function getRealIP(){
        $forwarded = request()->header("x-forwarded-for");
        if($forwarded){
            $ip = explode(',',$forwarded)[0];
        }else{
            $ip = request()->ip();
        }
        return $ip;
    }