PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
tinoda
Forum Commoner
Posts: 33 Joined: Mon Dec 29, 2008 3:32 am
Post
by tinoda » Wed Jan 07, 2009 12:44 pm
i have the following code below that i have constructed. the code enters in 10 names. How can I code it in such a way that a user will fail to enter the same name they have entered before. Danke Thanks:
Code: Select all
<?php
// messages.php
if (isset($_POST['message'])) {
if (file_exists('messageCount.txt')) {
$count = fopen('messageCount.txt','r+');
$n = fread($count,1024);
}
else {
$count = fopen('messageCount.txt','a+');
$n = 0;
}
if (($e = ($n + 1)) <= 10) {
rewind($count);
fwrite($count,$e);
fclose($count);
$fp = fopen('messages.txt', 'a');
fwrite($fp, "{$_POST['message']}<br />");
fclose($fp);
}
}
readfile('messages.txt');
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="message">
<input type="submit" value="Namen eingeben">
</form>
sergio-pro
Forum Commoner
Posts: 88 Joined: Sat Dec 27, 2008 12:26 pm
Post
by sergio-pro » Wed Jan 07, 2009 1:15 pm
Hallo
This code will check the name.
But do not ever use this code to save data on server. It is not flexible and safe.
Code: Select all
<?php
// messages.php
if (isset($_POST['message'])) {
if (file_exists('messageCount.txt')) {
$count = fopen('messageCount.txt','r+');
$n = fread($count,1024);
}
else {
$count = fopen('messageCount.txt','a+');
$n = 0;
}
$contents = file_get_contents('messages.txt');
$arr = split("<br />", $contents);
if (in_array($_POST['message'], $arr)) {
echo "<font color='red'>Error: name already exists</font><br/><br/>";
} else {
if (($e = ($n + 1)) <= 10) {
rewind($count);
fwrite($count,$e);
fclose($count);
$fp = fopen('messages.txt', 'a');
fwrite($fp, "{$_POST['message']}<br />");
fclose($fp);
}
}
}
readfile('messages.txt');
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="message">
<input type="submit" value="Namen eingeben">
</form>
Bitte please )
tinoda
Forum Commoner
Posts: 33 Joined: Mon Dec 29, 2008 3:32 am
Post
by tinoda » Wed Jan 07, 2009 1:33 pm
sergio-pro - thanks it really works. but why would it not be safe to use on the server
sergio-pro
Forum Commoner
Posts: 88 Joined: Sat Dec 27, 2008 12:26 pm
Post
by sergio-pro » Wed Jan 07, 2009 1:40 pm
There could be a problem if two (or more) users will try to add their names at the same time.
Use databases for that.