Avatar billede morphman Nybegynder
29. august 2005 - 18:17 Der er 18 kommentarer og
1 løsning

URL verifier..

Well, hey lads...

Okay, I need a big favour..I am trin' to make a url verifier in php. The sole purpose for this program like I said is to verify if the url exists or not. In other words if the url is correct. I am thinking that the script should be able to take a server url from user. Following it should add the filename which it will load from a database(could just be a .txt file). So, what the script
does is that it take the url given by the user like http://www.mydomain.com/ and adds the filename(ex: myfile.zip) loaded from the .txt file at the end of the given url. such as this http://www.mydomain.com/myfile.zip. If it's true it returns the full url to the user otherwise it continues it's search until the end of database...I am runnin' completely blank on this one..so, I really hope ye lads can help me out of this pickle..

cheers :)
Avatar billede morphman Nybegynder
29. august 2005 - 18:30 #1
Here is a little non functional prototype..this should give an idea:

http://i.domaindlx.com/morphman/eksperten/spm/643967/url.verifier.html
Avatar billede morphman Nybegynder
29. august 2005 - 18:33 #2
The radiobuttons sybomizes the sub directory to be scaned. Like if ZIP has been chossen then our previously given url look like this serveradr + dir + filename. such as this http://www.mydomain.com/ZIP/myfile.zip
Avatar billede morphman Nybegynder
29. august 2005 - 18:38 #3
Avatar billede coderdk Praktikant
29. august 2005 - 20:23 #4
Use a function like this:

    function pageExists( $url )
    {
        if ( preg_match( '-^http://([^/:]+)(:(d+))?(/.*)?-i', $url, $m ) )
        {
            $host = $m[1];
            $port = ( !empty( $m[3] ) ? $m[3] : 80 );
            $path = ( !empty( $m[4] ) ? $m[4] : '/' );
            if ( $fp = @fsockopen( $host, $port, $ern, $ers, 3.0 ) )
            {
                fwrite( $fp, "HEAD $path HTTP/1.0\nHost: $host\nConnection: Close\n\n" );
                $response = fread( $fp, 512 );
                fclose( $fp );
                if ( preg_match( '-HTTP/d.d (d+) .+-i', $response, $m ) )
                {
                    return ( $m[1] != '404' );
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }


Then loop through the data and call the function with the URL, simple ;)
Avatar billede morphman Nybegynder
30. august 2005 - 01:47 #5
Ohh..sorry fo rthe rather delayed reply mate..thank ye for ye helpin' hand ;)
However, it would be superb..if ye could make a..dummy script for me..I don't
know how to just add this bit in and make it work. I hope I am not askin' for
too much *sure I am, but I hope ye'll help me again*
Avatar billede coderdk Praktikant
30. august 2005 - 10:16 #6
erh, well you know how to make queries?

// connect to the database
// Also we assume that the user input was POST'ed here in "url"
$sql = "select file FROM files";
$qh = mysql_query( $sql ) or die( mysql_error() );
while ( $row = mysql_fetch_assoc( $qh ) )
{
  if ( pageExists( $_POST['url'] . $row['file'] ) )
  {
      echo $_POST['url'] . $row['file'] . " exists!<br>";
  }
  else
  {
      echo $_POST['url'] . $row['file'] . " does not exist...<br>";
  }
}
Avatar billede morphman Nybegynder
30. august 2005 - 12:28 #7
Well, yes, I do know how to make mySQL queries..however, I was thinkin' to keep it a little more simple then that...why not just use simple .txt files as a database?
Avatar billede coderdk Praktikant
30. august 2005 - 12:30 #8
well ok, just use something like:

$lines = file( 'file.txt' );
foreach ( $lines as $line )
{
  echo $_POST['url'] . $line . " ";
  if ( pageExists( $_POST['url'] . $line ) )
  {
      echo "exists!<br>";
  }
  else
  {
      echo "does not exist...<br>";
  }
}
Avatar billede morphman Nybegynder
30. august 2005 - 13:13 #9
Ahh..now we're talkin..let me see..if I can get this to work. :)
Avatar billede morphman Nybegynder
31. august 2005 - 00:24 #10
I've been wokrin' with the whole day..however, I can't seem to get it to work. would ye please tell me how am I suppose to put this in work with me current prototype?
Avatar billede coderdk Praktikant
31. august 2005 - 01:58 #11
Uhm, well just cut/paste most of what I've written:

<?php

  // This is the page your form should POST to!

    function pageExists( $url )
    {
        if ( preg_match( '-^http://([^/:]+)(:(d+))?(/.*)?-i', $url, $m ) )
        {
            $host = $m[1];
            $port = ( !empty( $m[3] ) ? $m[3] : 80 );
            $path = ( !empty( $m[4] ) ? $m[4] : '/' );
            if ( $fp = @fsockopen( $host, $port, $ern, $ers, 3.0 ) )
            {
                fwrite( $fp, "HEAD $path HTTP/1.0\nHost: $host\nConnection: Close\n\n" );
                $response = fread( $fp, 512 );
                fclose( $fp );
                if ( preg_match( '-HTTP/d.d (d+) .+-i', $response, $m ) )
                {
                    return ( $m[1] != '404' );
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

$lines = array_merge( file( 'database_zip.txt' ), file( 'database_rar.txt' ) );
foreach ( $lines as $line )
{
  echo $_POST['url'] . $line . " ";
  if ( pageExists( $_POST['url'] . $line ) )
  {
      echo "exists!<br>";
  }
  else
  {
      echo "does not exist...<br>";
  }
}

?>

Not that the URL that people should POST should end with a / - e.g. http://example.com/folder/
Avatar billede coderdk Praktikant
31. august 2005 - 01:58 #12
That is also assuming that database_zip.txt and database_rar.txt are in the same folder as the script.
Avatar billede morphman Nybegynder
06. september 2005 - 23:20 #13
I am rather sorry for this delayed respons...however, I was havin' some problems with me internet connection. All seems to be fine now. Still, I can't seem to get this script up and runin'..the it's not hosted to my server, I think ye problem could have been that all a long...But that sure shouldn't be ye problem. So, therfore..thank ye very much for those fast replies and here are ye point :D
Avatar billede morphman Nybegynder
06. september 2005 - 23:22 #14
lol..I just realized ye haven't given an answer ..I can't give ye any points..so I wait for ye answer.
Avatar billede coderdk Praktikant
07. september 2005 - 00:21 #15
hehhe ok :) maybe fopen wrappers isn't enabled on the server? Does this give you any output?

<? echo file_get_contents( 'http://www.google.com/' ); ?>
Avatar billede morphman Nybegynder
07. september 2005 - 21:13 #16
yeah, mate..I think so to..damn me webhost.
it gives me this error while test ye code
above.

[error]

Warning: file_get_contents(): URL file-access is disabled in the server configuration in /customers/********/********/httpd.www/test1.php on line 1

Warning: file_get_contents(http://www.google.com/): failed to open stream: no suitable wrapper could be found in /customers/********/********/httpd.www/test.php on line 1

[/error]
Avatar billede coderdk Praktikant
07. september 2005 - 22:30 #17
Damn, looks like they have disabled both the fopen wrappers and the socket functions :(
Avatar billede morphman Nybegynder
14. september 2005 - 17:08 #18
Yeah, it's b-one. (damn those bas.....) I guess, there isn't any other way, right?
Avatar billede morphman Nybegynder
14. september 2005 - 17:08 #19
Sorry for the delay, however, here are ye points :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester