Jump to content

Recommended Posts

I need to create a guestbook that will check for User Name duplicates and then will store in .txt file

 

 

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 9/15/2009 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>
<?php

error_reporting(E_ALL);


function validateName($GuestName)
{
if (strpos($GuestName, " ") !== FALSE)
    return true;
else
    return false;

}


function validateEmail($GuestEmail)
{ //validation emaIL
if ((strpos($GuestEmail, "@") !== FALSE) && (strpos($GuestEmail, ".") !== FALSE))
   return true;
else 
   return false;
}


function checkForDuplicates($GuestbookUser) // function check for duplicates.
{
$myFile = "./GuestbookFile.txt";
$fh = fopen($myFile, 'r');

if(filesize($myFile)>0)
{
	$theData = fread($fh, filesize($myFile));
	fclose($fh);
	$entries = explode("*", $theData);
	$i = 0;
	foreach ($entries as $entry)
	{
	 	$data = explode(",", $entry);
		if($data[0] == $GuestbookUser)
		{
		echo $data[0];
		  return true;
		}
		echo $i++;
	}
}
    return false;
}

$GuestName = $_POST["GuestName"];

//else if (validateName($_POST["GuestName"]) == FALSE)
if (validateName($_POST["GuestName"]) == FALSE)
   echo "<p> Your name is not valid. There is no space in between. Click BACK button and type a valid name.</p>";
else
   echo "<p>$GuestName</p>";

$GuestEmail = $_POST["GuestEmail"];
if (validateEmail($_POST["GuestEmail"]) == FALSE)
    echo "<p>Your email address is not valid. Click BACK button and type a valid email.</p>";
else
    echo "<p>$GuestEmail</p>"; 

$GuestbookUser = $_POST["GuestName"] && $_POST["GuestEmail"]; //checking for duplicates {mistake here}
if (checkforduplicates($GuestbookUser) == true) //function is here but it is still not working
   echo "<p>Your name is used in our guestbook. Please click your browsers BACK button to return and enter different name.</p>";
else
{
	$GuestbookFile = fopen("./GuestbookFile.txt", "r+");//writes data into a file variable for txt file
	fwrite($GuestbookFile, $GuestName.",".$GuestEmail."*");
	fclose($GuestbookFile);
}
?>
</body>
</html>

 

and this is what i got after entering user name which was "first time user "

Alex Mick

[email protected]

mjgx shxc

 

Your name is used in our guestbook. Please click your browsers BACK button to return and enter different name

Link to comment
https://forums.phpfreaks.com/topic/175044-debugging-problems/
Share on other sites

$GuestbookUser = $_POST["GuestName"] && $_POST["GuestEmail"]; //checking for duplicates {mistake here}

&& is a logical operator NOT concatenation (? I've never been able to spell that!).  Try

 

$GuestbookUser = $_POST["GuestName"] . "," . $_POST["GuestEmail"]; //checking for duplicates {mistake here}

Link to comment
https://forums.phpfreaks.com/topic/175044-debugging-problems/#findComment-922572
Share on other sites

Made comments in code

 

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Created on: 9/15/2009 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>
<?php

error_reporting(E_ALL);


function validateName($GuestName)
{
if (strpos($GuestName, " ") != FALSE)
    return true;
else
    return false;

}


function validateEmail($GuestEmail)
{ //validation emaIL
if ((strpos($GuestEmail, "@") != FALSE) && (strpos($GuestEmail, ".") != FALSE))
   return true;
else 
   return false;
}


function checkForDuplicates($GuestbookUser) // function check for duplicates. var = email only
{
   $myFile = "./GuestbookFile.txt";
   $fh = fopen($myFile, 'r');
   
   if(filesize($myFile)>0)
   {
      $theData = fread($fh, filesize($myFile));
      fclose($fh);
      $entries = explode("*", $theData);
      $i = 0;
      foreach ($entries as $entry)
      {
          $data = explode(",", $entry); // since your exploding (My Name,[email protected]) - only check data[1]

         if($data[1] == $GuestbookUser ) // CHANGED - from data[0] - checking for the actual email only
         {
         echo $data[1];
           return true;
         }
         echo $i++;
      }
   }
    return false;
}

$GuestName = $_POST["GuestName"];

//else if (validateName($_POST["GuestName"]) == FALSE)
if (validateName($GuestName) == FALSE)
   echo "<p> Your name is not valid. There is no space in between. Click BACK button and type a valid name.</p>";
else
   echo "<p>$GuestName</p>";

$GuestEmail = $_POST["GuestEmail"];
if (validateEmail($GuestEmail) == FALSE)
    echo "<p>Your email address is not valid. Click BACK button and type a valid email.</p>";
else
    echo "<p>$GuestEmail</p>"; 

//$GuestbookUser = $GuestName.",".$GuestEmail; //checking for duplicates {mistake here}
if (checkforduplicates($GuestEmail) == true) //function is here but it is still not working - CHANGED submit email only
   echo "<p>Your name is used in our guestbook. Please click your browsers BACK button to return and enter different name.</p>";
else
   {
      $GuestbookFile = fopen("./GuestbookFile.txt", "r+");//writes data into a file variable for txt file
      fwrite($GuestbookFile, $GuestName.",".$GuestEmail."*");
      fclose($GuestbookFile);
   }
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/175044-debugging-problems/#findComment-922860
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.