Advertisement
johnmalay89

kod nak buat sweet alert

Aug 28th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.60 KB | None | 0 0
  1. <?php
  2.  
  3. function ValidateEmail($email)
  4.  
  5. {
  6.  
  7.    $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
  8.  
  9.    return preg_match($pattern, $email);
  10.  
  11. }
  12.  
  13. if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'borang_log')
  14.  
  15. {
  16.  
  17.    $mailto = 'yourname@yourdomain.com';
  18.  
  19.    $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
  20.  
  21.    $subject = 'Website form';
  22.  
  23.    $message = 'Values submitted from web site form:';
  24.  
  25.    $success_url = './success.php';
  26.  
  27.    $error_url = './failed.php';
  28.  
  29.    $csvFile = "./formdata.csv";
  30.  
  31.    $error = '';
  32.  
  33.    $mysql_server = 'localhost';
  34.  
  35.    $mysql_database = '';
  36.  
  37.    $mysql_table = '';
  38.  
  39.    $mysql_username = '';
  40.  
  41.    $mysql_password = '';
  42.  
  43.    $eol = "\n";
  44.  
  45.    $boundary = md5(uniqid(time()));
  46.  
  47.  
  48.  
  49.    $header  = 'From: '.$mailfrom.$eol;
  50.  
  51.    $header .= 'Reply-To: '.$mailfrom.$eol;
  52.  
  53.    $header .= 'MIME-Version: 1.0'.$eol;
  54.  
  55.    $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
  56.  
  57.    $header .= 'X-Mailer: PHP v'.phpversion().$eol;
  58.  
  59.    if (!ValidateEmail($mailfrom))
  60.  
  61.    {
  62.  
  63.       $error .= "The specified email address is invalid!\n<br>";
  64.  
  65.    }
  66.  
  67.  
  68.  
  69.    if (!empty($error))
  70.  
  71.    {
  72.  
  73.       $errorcode = file_get_contents($error_url);
  74.  
  75.       $replace = "##error##";
  76.  
  77.       $errorcode = str_replace($replace, $error, $errorcode);
  78.  
  79.       echo $errorcode;
  80.  
  81.       exit;
  82.  
  83.    }
  84.  
  85.  
  86.  
  87.    $internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response");
  88.  
  89.    $message .= $eol;
  90.  
  91.    $message .= "IP Address : ";
  92.  
  93.    $message .= $_SERVER['REMOTE_ADDR'];
  94.  
  95.    $message .= $eol;
  96.  
  97.    $logdata = '';
  98.  
  99.    foreach ($_POST as $key => $value)
  100.  
  101.    {
  102.  
  103.       if (!in_array(strtolower($key), $internalfields))
  104.  
  105.       {
  106.  
  107.          $logdata .= ';';
  108.  
  109.          if (!is_array($value))
  110.  
  111.          {
  112.  
  113.             $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
  114.  
  115.             $value = str_replace(";", " ", $value);
  116.  
  117.             $logdata .= $value;
  118.  
  119.          }
  120.  
  121.          else
  122.  
  123.          {
  124.  
  125.             $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
  126.  
  127.             $logdata .= implode("|", $value);
  128.  
  129.          }
  130.  
  131.       }
  132.  
  133.    }
  134.  
  135.    $logdata = str_replace("\r", "", $logdata);
  136.  
  137.    $logdata = str_replace("\n", " ", $logdata);
  138.  
  139.    $logdata .= "\r\n";
  140.  
  141.    $handle = fopen($csvFile, 'a') or die("can't open file");
  142.  
  143.    $logtime = date("Y-m-d H:i:s;");
  144.  
  145.    fwrite($handle, $logtime);
  146.  
  147.    fwrite($handle, $_SERVER['REMOTE_ADDR']);
  148.  
  149.    fwrite($handle, $logdata);
  150.  
  151.    fclose($handle);
  152.  
  153.    $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;
  154.  
  155.    $body .= '--'.$boundary.$eol;
  156.  
  157.    $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
  158.  
  159.    $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
  160.  
  161.    $body .= $eol.stripslashes($message).$eol;
  162.  
  163.    if (!empty($_FILES))
  164.  
  165.    {
  166.  
  167.        foreach ($_FILES as $key => $value)
  168.  
  169.        {
  170.  
  171.           if ($_FILES[$key]['error'] == 0)
  172.  
  173.           {
  174.  
  175.              $body .= '--'.$boundary.$eol;
  176.  
  177.              $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
  178.  
  179.              $body .= 'Content-Transfer-Encoding: base64'.$eol;
  180.  
  181.              $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
  182.  
  183.              $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
  184.  
  185.           }
  186.  
  187.       }
  188.  
  189.    }
  190.  
  191.    $body .= '--'.$boundary.'--'.$eol;
  192.  
  193.    if ($mailto != '')
  194.  
  195.    {
  196.  
  197.       mail($mailto, $subject, $body, $header);
  198.  
  199.    }
  200.  
  201.    $search = array("ä", "Ä", "ö", "Ö", "ü", "Ü", "ß", "!", "§", "$", "%", "&", "/", "\x00", "^", "°", "\x1a", "-", "\"", " ", "\\", "\0", "\x0B", "\t", "\n", "\r", "(", ")", "=", "?", "`", "*", "'", ":", ";", ">", "<", "{", "}", "[", "]", "~", "²", "³", "~", "µ", "@", "|", "<", "+", "#", ".", "´", "+", ",");
  202.  
  203.    $replace = array("ae", "Ae", "oe", "Oe", "ue", "Ue", "ss");
  204.  
  205.    foreach($_POST as $name=>$value)
  206.  
  207.    {
  208.  
  209.       $name = str_replace($search, $replace, $name);
  210.  
  211.       $name = strtoupper($name);
  212.  
  213.       $form_data[$name] = $value;
  214.  
  215.    }
  216.  
  217.    $db = mysqli_connect($mysql_server, $mysql_username, $mysql_password) or die('Failed to connect to database server!<br>'.mysqli_error($db));
  218.  
  219.    mysqli_query($db, "CREATE DATABASE IF NOT EXISTS $mysql_database");
  220.  
  221.    mysqli_select_db($db, $mysql_database) or die('Failed to select database<br>'.mysqli_error($db));
  222.  
  223.    mysqli_query($db, "CREATE TABLE IF NOT EXISTS $mysql_table (ID int(9) NOT NULL auto_increment, `DATESTAMP` DATE, `TIME` VARCHAR(8), `IP` VARCHAR(15), `BROWSER` TINYTEXT, PRIMARY KEY (id))");
  224.  
  225.    foreach($form_data as $name=>$value)
  226.  
  227.    {
  228.  
  229.       mysqli_query($db ,"ALTER TABLE $mysql_table ADD $name VARCHAR(255)");
  230.  
  231.    }
  232.  
  233.    mysqli_query($db, "INSERT INTO $mysql_table (`DATESTAMP`, `TIME`, `IP`, `BROWSER`)
  234.  
  235.                VALUES ('".date("Y-m-d")."',
  236.  
  237.                '".date("G:i:s")."',
  238.  
  239.                '".$_SERVER['REMOTE_ADDR']."',
  240.  
  241.                '".$_SERVER['HTTP_USER_AGENT']."')")or die('Failed to insert data into table!<br>'.mysqli_error($db));
  242.  
  243.    $id = mysqli_insert_id($db);
  244.  
  245.    foreach($form_data as $name=>$value)
  246.  
  247.    {
  248.  
  249.       mysqli_query($db, "UPDATE $mysql_table SET $name='".mysqli_real_escape_string($db, $value)."' WHERE ID=$id") or die('Failed to update table!<br>'.mysqli_error($db));
  250.  
  251.    }
  252.  
  253.    mysqli_close($db);
  254.  
  255.    header('Location: '.$success_url);
  256.  
  257.    exit;
  258.  
  259. }
  260.  
  261. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement