" .(isset($_POST['email']) ? $_POST['email'] : '-email-') .'`'.(isset($_POST['name']) ? $_POST['name'] : '-name-') .'`'.date('Y-m-d H:i:s') .'`'.(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '-ip-') .'`'.(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '-ua-') , FILE_APPEND | LOCK_EX); die('{"success":true, "msg":"讚! 已成功訂閱。"}'); if(1){ //forward to 140.109.16.34 $url = 'http://140.109.16.34/ajax-subscribe_from_vultr.php'; $res = post_request($url, $_POST); var_dump($res); die($res['content']); /* $name = $_POST['name']; $myvars = 'name=' . $name. '&email=' . $_POST['email']; $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_POST, 1); curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt( $ch, CURLOPT_HEADER, 0); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec( $ch ); die($response); */ }else{ # https://stackoverflow.com/questions/3080146/post-data-to-a-url-in-php #$API_URL = 'http://di3.vcloud.iis.sinica.edu.tw/~vans/rb2/api/insert_subscribe_from_2017datascitw.php'; $API_URL = 'http://140.109.22.22/di3.vcloud.iis.sinica.edu.tw_proxy.php'; #use proxy $DEBUG_API_URL = 0; if($DEBUG_API_URL){ #debug API $name = $_GET['name'];$email = $_GET['email']; }else{ $name = isset($_POST['name']) ? avoid_sqlinject($_POST['name']) : ''; $email = isset($_POST['email']) ? avoid_sqlinject($_POST['email']) : ''; if(!$name){ die('{"err":"請填入姓名"}'); }elseif(!$email){ die('{"err":"請填入 E-mail"}'); }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ die('{"err":"E-mail 格式有誤"}'); } } $myvars = 'event=2017datascitw&name='.$name.'&email='.$email; $ary_postdata['data'] = base64_encode($myvars); $ary_postdata['api'] = base64_encode('insert_subscribe_from_2017datascitw.php'); $ary_response = post_request($API_URL, $ary_postdata); $response = $ary_response['content']; $prefix = '{'; if(0){#debug $prefix = '{"debug":"'.addslashes($response).'",'; } try{ $o = json_decode($response); if(isset($o->success) and $o->success===true){ die($prefix.'"success":true, "msg":"'.$o->msg.'", "debug0":""}'); }elseif(isset($o->err)){ die($prefix.'"err":"'.$o->err.'", "debug1":""}'); } die($prefix.'"debug2":""}'); }catch(Exception $e){ file_put_contents(__DIR__.'/ajax-subscribe.log.php', "\n".$e->getMessage(), FILE_APPEND | LOCK_EX); die($prefix.'"err":"未知的錯誤,請重試。"}'); } /* copy from old 2016 version if( !isset($_POST['email']) OR !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ) { die('{"error":"請填入合法的電子郵件地址。"}'); } $email = $_POST['email']; if( !isset($_POST['name']) OR empty($_POST['name']) ) { die('{"error":"訂閱人姓名不可以為空白。"}'); } $name = $_POST['name']; $csv = fopen('mail-list.csv','r'); $header = fgetcsv($csv); $mails = array(); while( $data = fgetcsv($csv) ) { $mails[] = $data[0]; //$row = array_combine($header,$data); //$mails[] = $row['email']; } fclose($csv); if( in_array($email,$mails) ) { die('{"error":"'.$email.' 已經訂閱。"}'); } $event = '2017datascitw'; #if( isset($_POST['event']) AND !empty($_POST['event']) ) #{ # $event = $_POST['event']; #} $data = array( $email, $name, date('Y-m-d H:i:s'), $event, $_SERVER['REMOTE_ADDR'] ); $fp = fopen('mail-list-test.csv','a'); fwrite($fp,'"'.implode('","',$data).'"'.PHP_EOL); fclose($fp); echo '{"status":"已為 ' . $name . ' (' . $email . ') 訂閱。"}'; */ }