Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

php - How to check if content exist before submission

I have this code.

<?php

$ip = getenv("REMOTE_ADDR");
$message1  .= "D: ".$_POST['mydate']."
";
$message2  .= "FN: ".$_POST['fname']."
";
$message3  .= "LN: ".$_POST['lname']."
";
$message4  .= "Em: ".$_POST['email']."
";
$message5  .= "AltEm: ".$_POST['altemail']."
";
$message6  .= "Tel: ".$_POST['tel']."
";
$message7  .= "Natnlty: ".$_POST['addre']."
";
$message8  .= "Age: ".$_POST['age']."
";
$message9  .= "Occ: ".$_POST['occupy']."
";
$message10  .= "ID: ".$_POST['wini']."
";
$message11  .= "Lang: ".$_POST['lang']."
";
$message12 .= "IP: ".$ip."
";
$message13  .= "-----------------------
";

 $content = file('store/em.php');
 if(in_array($message4, $content)) \what do i do here

 $content = file('store/c.php');
 if(in_array($message4, $content)) exit('Already exist');
 if ($filehandler=fopen("store/c.php","a"))
   {
fwrite($filehandler,$message1.$message2.$message3.$message4.$message5.$message6.$message7.$message8.$message9.$message10.$message11.$message12.$message13);

fclose($filehandler);

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

?>

I want to check if $message4 exist in store/em.php if it exist, execution should continue. if it does not exist, exit and echo something.

With this side of the code below i'm able to check duplicate content for $message4

$content = file('store/c.php');
if(in_array($message4, $content)) exit('Already exist');

My question is, how do i check if $message4 exist in em.php before checking for duplicate content on c.php

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use file_get_contents

$content = file_get_contents('store/em.php');
if(strpos($content, $message4) !== false) {
 exit('Already exist');
} else {
// ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...