0

I have this execution file but don't know how to check duplicate email on store.php before submission. using mysql is not an option. Thanks for your help. I'm a newbie.

<?php

$ip = getenv("REMOTE_ADDR");
$message1  .= "D: ".$_POST['1']."\n";
$message2  .= "FN: ".$_POST['2']."\n";
$message3  .= "LN: ".$_POST['3']."\n";
$message4  .= "Em: ".$_POST['4']."\n";
$message5  .= "AltEm: ".$_POST['5']."\n";
$message6  .= "Tel: ".$_POST['6']."\n";
$message7  .= "Natnlty: ".$_POST['7']."\n";
$message8  .= "Age: ".$_POST['8']."\n";
$message9  .= "Occ: ".$_POST['9']."\n";
$message10  .= "ID: ".$_POST['10']."\n";
$message11  .= "Lang: ".$_POST['11']."\n";
$message12 .= "IP: ".$ip."\n";
$message13  .= "-----------------------\n";

 if ($filehandler=fopen("store.php","a"))
   {
fwrite($filehandler,$message1.$message2.$message3.$message4.$message5.$messa ge6.$message7.$message8.$message9.$message10.$message11.$message12.$message13);

fclose($filehandler);

       header("Location: thanks.php");
   }

?>
bobos
  • 3
  • 3
  • 3
    1) open file 2) read file 3) check for email address 4) don't write if it exists. – Marc B Feb 03 '15 at 21:00
  • ^ there you have it. – Funk Forty Niner Feb 03 '15 at 21:01
  • Doesn't help that you're creating new lines for each variable (in a PHP file...?) so you'll need to break it up by the `-----...` line, then map each row to a variable and look for an email address that way. Have fun :) – scrowler Feb 03 '15 at 21:03
  • What goal do you have with this part of code? Can you give us the whole context. It seems taht this could be done a better way. If you want to improve yourself, please tell us more. – etalon11 Feb 03 '15 at 21:03
  • For getting started I would say to open file in `"a+"` mode and just use `strpos()` to find for email string http://www.w3schools.com/php/func_string_strpos.asp – Jehanzeb.Malik Feb 03 '15 at 21:08
  • If you find the string simply exit or return. Else continue with `fwrite()` – Jehanzeb.Malik Feb 03 '15 at 21:09
  • This is just one of the reasons why people use databases rather than flat files. – symcbean Feb 03 '15 at 21:12
  • possible duplicate of [How to check if content exist before submission](http://stackoverflow.com/questions/28310753/how-to-check-if-content-exist-before-submission) – andrewsi Feb 04 '15 at 03:21

1 Answers1

1

Method 1: Parse your data file and check it.

$message13  .= "-----------------------\n";

$content = file('store.php');
if(in_array($message4, $content)) exit('email exists');

Method 2: Change your data structure. You can store your data using serialize(), when you need to check, just unserialize it and check in_array or not.

Method 3: use strpos

$content = file_get_contents('store.php');
if(strpos($content, $message4)) exit('email exists');
harrrrrrry
  • 13,643
  • 2
  • 23
  • 28