Read a text file?

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

User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Read a text file?

Post by m0u53m4t »

Im making a very simple register program for VB where there is a webbrowser where you enter you're serial number. The program then reads when you've got to the success page (in this case http://www.google.com) then the program accepts you. I want to make it read a file (for example: http://me.myhost.com/key.txt and compare $serial to the contents. How could I do that?

This is my code so far:

Code: Select all

<?php 
$serial = $_GET["serial"];
if (($serial == '1234-5678-91011')) { 
header( 'Location: http://www.google.com' ) ;
} 
else { 
   echo 'Incorrect serial number'; 
} 
?>
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Please try this.

Code: Select all

<?php

$serial = $_GET["serial"]; 
// get contents of a file into a string
$filename = "http://me.myhost.com/key.txt";
$handle = fopen( $filename, "rb");
while( !feof( $handle )){
    $line = fgets( $handle ); //read line by line
    if($serial==$line){
       return(true); //if serial matches with line, return true
       fclose($handle); //close the file
    }
}

return(false);
?> 
Regards,
Dibyendra
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

I'm getting this error:

Code: Select all

Warning: fopen(): URL file-access is disabled in the server configuration in /home/freehost/t35.com/j/u/juniorfiles/reg.php on line 6

Warning: fopen(http://cardiffhigh.cardiff.sch.uk/~jamie.sargant/key.txt): failed to open stream: no suitable wrapper could be found in /home/freehost/t35.com/j/u/juniorfiles/reg.php on line 6

Warning: feof(): supplied argument is not a valid stream resource in /home/freehost/t35.com/j/u/juniorfiles/reg.php on line 7

Warning: fgets(): supplied argument is not a valid stream resource in /home/freehost/t35.com/j/u/juniorfiles/reg.php on line 8
Then the last two just loop and keep giving them again.
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

t35 doesnt let you use fopen i guess, but try setting permissions of the .txt file to 777
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Its hosted through dav. How do I CHmod it from there?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

By having a script that directly reads the serial numbers list you'll compromize all the valid ones..
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

The file will only have one bit of text in (the entire contents may be "1234-5678-9101"). I just need the php to compare the user entered string entirely to the contents of that file.
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

if the text file will have only 1 bit of text, why not just put the serial in the script?
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

...good point... :oops: ... how would I do it if it was more than one?
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

You could put them all in an array and then check the input against it.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

How can I put them in an array?
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

Code: Select all

$serial  = array(serials => '12345', '67890', '09876');
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Is there any advantage of doing that over doing this:

Code: Select all

if (($serial == '12345' or '67890' or '09876')) {
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

I don't know if there is any advantage or disadvantage in doing it this way,
but I will look better.
And you can have access to all the serials later in the Script if you need them.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

This is my entire script:

Code: Select all

<?php
$serial = $_GET["serial"];
if (($serial == '7JKD-F982-J894-NH7I-8KDF' or '43T7-9H2O-348Y-UVH2-80VN' or '79EU-IDJH-J09F-UAWE-HRF9')) { // Valid serial keys
header( 'Location: http://www.google.com' ) ;
}
else {
header( 'Location: http://www.altavista.com' ) ;
}
if (($serial == '7K7D-SGF7-J894-NH7I-J8G5')) { // Blacklisted serials
header( 'Location: http://www.yahoo.com' ) ;
}
?>
and if the serial is right, it goes to google, if its black listed it goes to Yahoo.
Post Reply