Jump to content

debugging problems


pinochio

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.