justsomeone wrote:

does your webserver user (possibly apache or nobody) have write access to the file and directory?

Try creating an empty database.txt file and set the permissions on it to 777. You may also want to create this file outside of the webroot.

I use badblue currently for testing. The permissions dont need to be fooled with on windows servers so I dont know what the problem seems to be.

Iv tested simple read, write scripts from other people and they work. They look almost the same as mine so I have a feeling it has somthing to do with the way I'm triggering the write code or maby just a simple error with signs somewhere.

Heres the forum part.

        <p><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="/">Click 
          here to go straight to the main page</a></font></p>
        <form METHOD="GET" name="NewMessage" action="index.php">
<?php

echo"
	<input type='hidden' name='list' value='no'>
	<font size='1' face='Verdana, Arial, Helvetica, sans-serif'> This is 
	server $word_number of $word_count. If you'd like to add another adserver to the database,
	type it into the box...<br>
	<input type='text' name='newadserver' size='80'>
	<input type='submit' name='Submit' value='Add'>
	</font> 
	</form>
        <br>";
	if ($list=="no") {
	echo"
        <font size='1' face='Verdana, Arial, Helvetica, sans-serif'>to 
        view all Adservers click <a href='$PHP_SELF?list=yes'>here</a></font>";
	}

if ($list=="yes") { 
echo "<pre>";
include "database.txt";
echo "</pre>";
}
?>

If this dosnt help Ill just post the full code and let you guys look it over.

    Are you sure if it is actually going into the if-statement?
    In other words, does it echo "The server ---- was added to the database"?
    I am wondering if there may be something wrong with your if-statement? I could be wrong but it seems weird to me.

    if ((!$newadserver == "") || (!$newadserver=="localhost") || (!$newadserver=="www.yahoo.com") || (!$newadserver=="www.google.com"))

    ...I think your ! should be outside of a parens and you should be using ands instead of ors?
    Should it be like this?

    if (!($newadserver == "") && !($newadserver=="localhost") && !($newadserver=="www.yahoo.com") && !($newadserver=="www.google.com"))

    I don't know, just a suggestion.
    But the main question is, does it say "The server ---- was added to the database"?

    Also you may want to try just writing regular text like "blah" to the file and see what happens.

    Heather

      hi

      Save a backup
      of your original php above as 'scriptname_default.php'

      Then do some changes to test simple fputs() operation in your code above,
      without all these conditions
      if( $newsad == that ) and if( $newsad == those ) and if( this )

      For example, you can do something like this for a nice test
      Note I have turned on full
      error_reporting (E_ALL);

      in case php has something to inform us

      <?php
      
      error_reporting (E_ALL);
      
      $newadserver = "ad-spamming.google.com";
      $file = "database.txt";      // database message file 
      
      $wordarray = file( $file ); 
      
      $fd = fopen( $file, "a+" ); 
      fputs ( $fd, "127.0.0.1" . $newadserver . "\r\n" ); 
      fclose( $fd );
      
      echo "first line in <b>database.txt</b> file:<br>{$wordarray[0]}<br>";
      echo "<h4>The server \"$newadserver\" was added to the database</h4>"; 
      echo "The end<br>"; 
      
      exit; //end of add new adserver process 
      
      ?>
        halojoy wrote:

        hi

        Save a backup
        of your original php above as 'scriptname_default.php'

        Then do some changes to test simple fputs() operation in your code above,
        without all these conditions
        if( $newsad == that ) and if( $newsad == those ) and if( this )

        For example, you can do something like this for a nice test
        Note I have turned on full
        error_reporting (E_ALL);

        in case php has something to inform us

        <?php
        
        error_reporting (E_ALL);
        
        $newadserver = "ad-spamming.google.com";
        $file = "database.txt";      // database message file 
        
        $wordarray = file( $file ); 
        
        $fd = fopen( $file, "a+" ); 
        fputs ( $fd, "127.0.0.1" . $newadserver . "\r\n" ); 
        fclose( $fd );
        
        echo "first line in <b>database.txt</b> file:<br>{$wordarray[0]}<br>";
        echo "<h4>The server \"$newadserver\" was added to the database</h4>"; 
        echo "The end<br>"; 
        
        exit; //end of add new adserver process 
        
        ?>

        hehe, Like I said before I tested other small fopen fwrite scripts and they all seem to work, including yours.

        "first line in database.txt file:

        The server "ad-spamming.google.com" was added to the database
        The end"

        Looking in the database.txt I found 127.0.0.1ad-spamming.google.com without a space 🙁 but it still wrote to file.

        So It looks like my conditions are whats messing it up. I still need a few sites listed as good so they wont be added to the list of ad servers. Is there any possible way to keep the code that I have and add a 2nd list of good sites or somthing for the php script to parse through.

        For ex I submit www.google.com the php script parses through a list to see if google.com is a good site. If it is echo "Sorry the site you have tryed to submit is in the whitelist database" else "Success, the adserver has been added to our database"

        or somthing to that effect. If it seems to much of a wast I need at least 3 or 4 spots to block 4 good sites from being added to the list. Such as localhost and my web server.

        Oh and to answer raspberryh's Question. No it does not say added to database. It just refreshes the site. 🙁

          Did you try my if-statement I gave you?

            raspberryh wrote:

            Did you try my if-statement I gave you?

            Ya, it didnt refresh the page but it did give me a white 404 page.

              You should try it with if(true) and then see if it still goes to the 404 page. So you know if the error is something that it is executing after it evaluates the if statement to true.

                raspberryh wrote:

                You should try it with if(true) and then see if it still goes to the 404 page. So you know if the error is something that it is executing after it evaluates the if statement to true.

                Im a bit confused. I think its messing up right when its evaluating. In a few mins im gonna try taking out the conditions and just test without them to see if that helps.

                  raspberryh wrote:

                  I could be wrong but it seems weird to me.

                  if ((!$newadserver == "") || (!$newadserver=="localhost") || (!$newadserver=="www.yahoo.com") || (!$newadserver=="www.google.com"))

                  Heather

                  I've tried to work out what the actual impact of the above if conditions would be, and I can't figure it out with any confidence, but I think it's likely to be very different from what you expect.

                  If you only want to go into the IF statement if $newadserver does not have any of the values listed, you could use raspberyyh's suggestion:

                   if (!($newadserver == "") && !($newadserver=="localhost") && !($newadserver=="www.yahoo.com") && !($newadserver=="www.google.com")) 
                  

                  or

                   if (($newadserver != "") && ($newadserver !="localhost") && ($newadserver != "www.yahoo.com") && ($newadserver !="www.google.com")) 
                  

                  or you could even use an array of "blocked" values:

                  $blockedServers = array("", "localhost", "www.yahoo.com", "www.google.com");
                  if in_array($newadserver, $blockedServers)
                  

                  Whatever you decide, a good debugging trick would be to make sure that you're getting as far as the attempt to write to the file:

                  //temporary debug echo statement:
                      echo ("about to try to write $newadserver to file...");
                      fputs($fd,"127.0.0.1".$newadserver."\r\n");
                      echo("<h4>The server \"$newadserver\" was added to the database</h4>"); 
                  

                  The above echo will help establish for certain whether the logic is wrong (and you're not even getting to the fputs line), or whether something strange is happening with the filehandling.

                  When you run the script, watch out for the two echo's above:

                  If neither echo appears
                  Your logic may not be doing what you expect - you're not even getting to the key area of the code.

                  If only the first echo appears
                  There's something wrong with the fputs - it looks fine syntactically, though.

                  If both echos appear
                  Your logic is fine and your fputs isn't broken. Possibilities here are that you don't have write permission to the file (you seem to have ruled this out), or could it be that the file has ended up in a different directory - maybe do a search across your system for it.

                    @ - I recoded the sites part to

                     $blockedServers = array("", "localhost", "www.yahoo.com", "www.google.com"); 
                    if (!in_array($newadserver, $blockedServers))  

                    I also added an echo above that and a few other places. When I click the submit button I still get sent to a white 404 page. Im guessing my logics wrong? lol anyway I figure to bring some light on the subject Ill just post the full source so you guys can test it for your selves. The one php page along with a blank database.txt file is in the ZIP.

                      Hiya DeathFireD,

                      Sorry, but I can't go through it in detail - what do you mean by a "white 404" page? A 404 page says "file not found". A blank white screen usually indicates that there is broken syntax somewhere, or a runtime error and that PHP has been configured not to display errors.

                      Check your php.ini settings to see if you can display errors, and then you should get a nicve message wsaying what line of the script is causing the problem.

                        go here to see what I mean. http://ordway.ath.cx:5121/TDA/Site/adserver/test/adserver2.php

                        Its a link to the script. Its not anything to do with the php.ini becuse normaly when theres an error I get a big white page but this is a 404. So your right theres got to be somthing wrong with whatever happends after I click submit. After trying to add asd.com and clicking submit the url changes to this http://ordway.ath.cx:5121/TDA/Site/adserver/test/index.php?list=no&newadserver=asd.com and this is where I get the 404.

                          I can't check it out. The fact that you're using a non-standard port means the dayjob won't let me access it 🙁

                            justsomeone wrote:

                            I can't check it out. The fact that you're using a non-standard port means the dayjob won't let me access it 🙁

                            Ahhh pooding sticks 🙁. Well when you have time try looking through the source. Its not that long theres only like 1 part that you should look at and thats the code that I posted on here.

                            If Anyone can help please do so Iv been fooling with the code for the past 2 days and cant seem to get it to write a new server to the text file 🙁

                              🙂

                              I unpacked your zip to my php server working folder
                              and did some ( well a lot of rewrite ) and eventually got it to work

                              not perfect, like I would prefer, but
                              there is some job left over for you

                              main issue was
                              you did not read $_GET[ 'variables' ]
                              into those variables you test in your IF(conditions )
                              this we have to do
                              when we run modern secure way, with globals = OFF

                              the in_array test bit doesnt work
                              probably because it does not test if submitted string is a part of total line in array(x)

                              I try to separate HTML Display from PHP Code
                              all testing and so is PHP in beginning

                              while end part is HTML
                              with only a few php lines

                              Here is my reworked code
                              took me surely 2-3 hours!

                              <?php
                              error_reporting(E_ALL);
                              
                              $info = "";
                              if (isset( $_GET['newadserver']) ) {
                              // checking, and possibly adding a new Ad servers
                              
                              $newadserver = trim( $_GET['newadserver'] );
                              $newadserver = stripslashes($newadserver);
                              $newadserver = strip_tags($newadserver);
                              $blockedServers = array("", "localhost", "www.yahoo.com", "www.google.com");
                              
                              $file = "database.txt";	  // database message file
                              $wordarray = file($file);
                              if (!in_array( $newadserver. "\r\n", $wordarray, TRUE )) { // word not already there
                                  $info .= "About to try to write $newadserver to file...<br>";
                                  if (!in_array( $newadserver, $blockedServers )) { // don't accept empty message
                                      $fd = fopen($file, "a+");
                              	  fputs( $fd, "127.0.0.1" . " " . $newadserver. "\r\n" );
                                      fclose ($fd);
                              	  $info .= "<h4>The server \"$newadserver\" was added to the database</h4>";
                                  }
                                  else {
                                      if ($newadserver == "")
                              	      $info .= "<h4>You tried to enter a blank entry</h4>";
                                      else
                                          $info .= "<h4>Sorry, \"$newadserver\" is blocked.</h4>";
                                  }
                              }
                              else {
                                  $info .= "<h4>The server \"$newadserver\" already exists in the vast Adserver database</h4>";
                              }
                              
                              } //end of add new adserver process
                              
                              //open wordlist file
                              $file = "database.txt"; // words file
                              $words = file($file);  // put each word in the array
                              
                              $list="";
                              if (isset($_GET['list'])) $list = $_GET['list'];
                              
                              $word = "";
                              $word_number = 0;
                              $word_count = count($words);
                              if ($word_count) {
                              // if (!isset($message)) {
                              $word_number = rand( 1, $word_count ); // show a random message
                              // } else {
                              // $word_number = $message; // show requested message
                              // }
                              
                              $word = $words[$word_number-1]; // position in array is $word_number-1
                              $word = ereg_replace("\n", "", $word);
                              }
                              
                              //////////////////////////////////////////////////////////
                              // Begin HTML output /////////////////////////////////////
                              
                              header("Expires: Mon, 29 Aug 2005 05:00:00 GMT");// Date in the past
                              header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                              header("Cache-Control: no-store, no-cache, must-revalidate");
                              header("Cache-Control: post-check=0, pre-check=0", false);
                              header("Pragma: no-cache");
                              ?>
                              <html>
                              <head>
                              <title>Adserver Submit</title>
                              <style type="text/css">
                              <!--
                              a {  color: #999999; text-decoration: none}
                              a:hover {  color: #FFFFFF}
                              -->
                              </style>
                              </head>
                              <body bgcolor="#000000" text="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
                              <?php
                              echo "<center><font face='Verdana, Arial, Helvetica, sans-serif'>";
                              echo $info;
                              echo "</font></center>";
                              ?>
                              <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
                                <tr> 
                                  <td> 
                                    <div align="center"> 
                                      <table width="600" border="0">
                                        <tr> 
                                          <td> 
                                            <center>
                                               <font size="6" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF"><b>
                              <?php
                              // uncomment for debugging
                              // echo("<p>Word is $word</p>");
                              // echo("<p>Word number $word_number</p>");
                              
                              echo "$word";
                              ?>
                                           </b></font><br>
                                              <br>
                                            </center>
                                          </td>
                                        </tr>
                              </table>
                                      <img src="adserver.png" width="379" height="120" border="0"><br>
                                      <p><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="./">Click 
                                        here to go straight to the main page</a></font></p>
                              
                                <form METHOD="GET" name="NewMessage" action="">
                              <?php
                              
                              echo"
                              
                              <font size='1' face='Verdana, Arial, Helvetica, sans-serif'> This is 
                              server $word_number of $word_count. If you'd like to add another message to the database,
                              type it into the box...<br>
                              <input type='text' name='newadserver' size='80'>
                              <input type='submit' name='Submit' value='Add'>
                              </font> 
                              </form>
                                  <br>";
                              if ($list != "yes") {
                              echo"
                                  <font size='1' face='Verdana, Arial, Helvetica, sans-serif'>to 
                                  view all Adservers click <a href='{$_SERVER["PHP_SELF"]}?list=yes'>here</a></font>";
                              }
                              
                              if ($list=="yes") { 
                              echo "<pre>filedata:<br>";
                              include "database.txt";
                              echo "</pre>";
                              }
                              ?>
                              	    <p><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><a href="http://www.tda.tech.nu" target="_blank">http://www.tda.tech.nu</a></font></p>
                                    </div>
                                  </td>
                                </tr>
                              </table>
                              </body>
                              </html>
                              

                              halojoy
                              🙂
                              the php practical coding worker

                                omg you rock man! It all works. There are a few small bugs here and there such as the "click here to view all entrys in database" If you click it, it adds whatever you typed in the input box again and again :queasy: . Other then that the submit works great. I guess Ill take some time looking over that click to view all thing to try and get it to just display everything in the database.txt

                                thank you all so much for your help.

                                edited: Well the code does work and it works great might I add, I fixed the display all adservers code (cough changed the link to index.php?) hehe. But I did notice somthing. When going through testing if adservers would be added if they where already in the database I noticed hell it ads them no matter what. I submitted ad.comtomysite.com like 8 times. I think the php script is looking at the start aka. 127.0.0.1 and not whats after it. For example if I type 127.0.0.1 it comes up with sorry site already in database. Any ideas?

                                  the in_array test bit doesnt work
                                  probably because it does not test if submitted string is a part of total line in array(x)

                                  as I mentioned it does not work

                                  it is this test we should change

                                  if (!in_array( $newadserver. "\r\n", $wordarray, TRUE )) { // word not already there
                                  

                                  because one line
                                  $wordarray[0] is "127.0.0.1 website.com\r\n"
                                  and
                                  $newadserver = "website.com"
                                  so test is done if
                                  "127.0.0.1 website.com\r\n" = "website.com\r\n"
                                  and it is not

                                  we should use a test for if "website.com" is a part within of any line ( $wordarray[x] ) in database.txt

                                  we should need some help
                                  i am no good with these string functions

                                  but I will test if this change will work

                                  <?php
                                  error_reporting(E_ALL);
                                  
                                  $info = "";
                                  
                                  if (isset( $_GET['newadserver']) ) {
                                  // checking, and possibly adding a new Ad servers
                                  
                                  $newadserver = trim( $_GET['newadserver'] );
                                  $newadserver = stripslashes($newadserver);
                                  $newadserver = strip_tags($newadserver);
                                  
                                  if ($newadserver == "")
                                  	 $info .= "<h4>You tried to enter a blank entry</h4>";
                                  else {
                                     $blockedServers = array("localhost", "www.yahoo.com", "www.google.com");
                                     if ( in_array( $newadserver, $blockedServers )) 
                                         $info .= "<h4>Sorry, \"$newadserver\" is blocked.</h4>";
                                  }
                                  
                                  if (empty( $info )) {
                                      $file = "database.txt";	  // database message file
                                      $wordarray = file($file);
                                  
                                  foreach ( $wordarray as $line ) {
                                      if ( strstr( $line, $newadserver ) )  { 
                                          $info .= "<h4>The server \"$newadserver\" already exists in the vast Adserver database</h4>";
                                          break;  // jump out of foreach loop
                                      }
                                  }
                                  }
                                  
                                  if (empty( $info )) { // All is well, save newadserver
                                          $fd = fopen($file, "a+");
                                  	  fputs( $fd, "127.0.0.1" . " " . $newadserver. "\r\n" );
                                          fclose ($fd);
                                  	  $info .= "<h4>The server \"$newadserver\" was added to the database</h4>";
                                  }
                                  
                                  } //end of add new adserver process
                                  
                                  //open wordlist file
                                  $file = "database.txt"; // words file
                                  $words = file($file);  // put each word in the array
                                  
                                  $list="";
                                  if (isset($_GET['list'])) $list = $_GET['list'];
                                  
                                  $word = "";
                                  $word_number = 0;
                                  $word_count = count($words);
                                  
                                  if ($word_count) {
                                  // if (!isset($message)) {
                                  $word_number = rand( 1, $word_count ); // show a random message
                                  // } else {
                                  // $word_number = $message; // show requested message
                                  // }
                                  
                                  $word = $words[$word_number-1]; // position in array is $word_number-1
                                  $word = ereg_replace("\n", "", $word);
                                  }
                                  
                                  // here comes HTML code
                                  // Begin HTML output
                                  ?>

                                  might work.
                                  might not, but in such case, sure should be easy to fix
                                  by some of our more experienced friends around here

                                  As you can see $info is not only used for display information
                                  but also serve as a variable, that signals, if everything is okay or not
                                  and used like
                                  if ( empty( $info ) ) do this

                                  I prefer this, before creating another variable
                                  or do some complicated if (this && that || not this)
                                  that tells if things should be done


                                  another thing I noticed
                                  was you do not have anywhere in form an <INPUT
                                  where $message ( $word_number ) get a value
                                  so, will always be a random website shown
                                  I commented that code out, but you may want to use it

                                  about function [man]strstr[/man] in php manual

                                  this is about the only in-string function I use
                                  an example

                                  /****
                                   say we want to test what sort of files we have in a directory
                                  if files has this or that extension: .exe .php .txt .dat ...
                                  maybe list only all text files
                                  ****/
                                  
                                  // do a loop and test every file like this
                                  if ( strstr( $filename, ".txt" ) ) echo "$filename <br>";
                                  
                                  // will produce a list of all textfiles, but no other files

                                  /halojoy - gone testing his new strstr adserver code now
                                  🆒

                                    halojoy wrote:

                                    as I mentioned it does not work

                                    it is this test we should change

                                    if (!in_array( $newadserver. "\r\n", $wordarray, TRUE )) { // word not already there
                                    

                                    another thing I noticed
                                    was you do not have anywhere in form an <INPUT
                                    where $message ( $word_number ) get a value
                                    so, will always be a random website shown
                                    I commented that code out, but you may want to use it

                                    about function [man]strstr[/man] in php manual

                                    this is about the only in-string function I use
                                    an example

                                    /****
                                     say we want to test what sort of files we have in a directory
                                    if files has this or that extension: .exe .php .txt .dat ...
                                    maybe list only all text files
                                    ****/
                                    
                                    // do a loop and test every file like this
                                    if ( strstr( $filename, ".txt" ) ) echo "$filename <br>";
                                    
                                    // will produce a list of all textfiles, but no other files

                                    /halojoy - gone testing his new strstr adserver code now
                                    🆒

                                    Ahh ok I was wondering why it wasnt working at all. Btw the code at the bottom where you said its random and it should be taken out..I know its random. Right now its supposed to show a random adserver everytime the site is refreshed and it does just that. Its not needed but I just put it there as a test at a erly time. I hope this new source works without any issues hehe.

                                      @ - I tested it and it worked great! I tryed adding in a server in the blocked list and that part worked fine, then I tryed adding a server already in the database and that worked fine. So everything is working now! Thanks so much. Im gonna run a few more tests before I make this thread as Resolved Just incase somthing pops up.

                                        okay

                                        it was interesting see your ideas
                                        in this script
                                        i might have use for some of it in future

                                        good luck in php coding

                                        everything new takes time to learn
                                        they say:
                                        "Big City of Roma in Italy
                                        was not built in one day"

                                        true!!

                                        🙂
                                        halojoy - old man, closer to 100 than to 0 years of age
                                        - so things takes even longer to learn
                                        - not like them young no more, them learning so quickly
                                        🙂

                                          Write a Reply...