1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Buying $10 quick (as in now) via PayPal to the first person to fix this PHP issue!!

Discussion in 'Programming' started by medialab, Sep 26, 2012.

  1. #1
    Hi Everyone!

    Ok so here is my issue, I have a small 1 field textbox form that a user will enter their code into. The PHP script below is suppose to check a plain .txt file on the server for a matching code. The text file has 10,000 random 6-7 digit alphanumeric codes in it (each one with a return break). The php code below is suppse to (and technically does) check the file to see if the number is there and send them to page A if it is and page B if it doesn't. The script works however there is one small problem - If a user enters 234 and one of the codes is A2349J it directs the user to a success page, it found the match, however it did not find the WHOLE match, just a partial. I need the script to make sure that it matches 100% and if not to go to the not found page. First user to answer this problem (via POST) for me gets $10 in their paypal account, I will user your code to check and then post results.

    <?php
    		// Filename
    		$file = "file.txt";
    		// Correct Number
    		$loc_correct = "http://www.website.com/success.php";
    		// Wrong Number
    		$loc_wrong = "http://www.website.com/notfound.php";
    
    			if (isset($_POST['number'])) {
    
    				// Set Number
    				$number = $_POST['number'];
    
    				// Regezx pattern that matches exact word
    				$patt = '/(?:^|[^a-zA-Z])'. preg_quote($number, '/'). '(?:$|[^a-zA-Z])/i';
    
    				// Reads File
    				$file = file_get_contents($file);
    				
    				// Check if $number exits in $file
    				if (preg_match($patt, $file)) {
        			
        			header('Location: '.$loc_correct);
    				exit;
      }
      
      header('Location: '.$loc_wrong);
      exit;
    }
    
    ?>
    Code (markup):

     
    medialab, Sep 26, 2012 IP
  2. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    If so I would do the following, replace

    // Regezx pattern that matches exact word
    				$patt = '/(?:^|[^a-zA-Z])'. preg_quote($number, '/'). '(?:$|[^a-zA-Z])/i';
    
    				// Reads File
    				$file = file_get_contents($file);
    				
    				// Check if $number exits in $file
    				if (preg_match($patt, $file)) {
    PHP:
    With

    
    // Reads File
    $file = file_get_contents($file);
    $lines = explode("\r",$file);
    $found = false;
    foreach($lines as $line) {
    	if(!$found) {
    		if($line == $number) {
    			$found = true;
    		}
    	}
    }
    if($found) {
    
    PHP:
     
    Last edited: Sep 26, 2012
    Scoding, Sep 26, 2012 IP
  3. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #3
    The codes are in this format:


    A849F5
    B8F9RJ
    83NR856
    JD7394M

    They each have a return break, so 1 long list with 10,000 lines
     
    medialab, Sep 26, 2012 IP
  4. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Yep updated my code, misread the thread in the first place, if my code doesn't work I will need the actual file sample, as the line breaks are never the same
     
    Scoding, Sep 26, 2012 IP
  5. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #5
    Currently your code goes to the not found page every time - even with the real code.
     
    medialab, Sep 26, 2012 IP
  6. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #6
    And the code below is exactly identical to the full PHP file, there is nothing else in it and line breaks and tabs have been preserved.
     
    medialab, Sep 26, 2012 IP
  7. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    Not found? Right.. Do this then please, replace all the code with

    
    <?php
    // Filename
    $file = "file.txt";
    // Correct Number
    $loc_correct = "http://www.website.com/success.php";
    // Wrong Number
    $loc_wrong = "http://www.website.com/notfound.php";
    
    if (isset($_POST['number'])) {
    
    // Set Number
    $number = $_POST['number'];
    // Reads File
    $file = file_get_contents($file);
    $lines = explode("\r",$file);
    $found = false;
    foreach($lines as $line) {
        if(!$found) {
            if($line == $number) {
                $found = true;
            }
        }
    }
    if($found) { 
    	header('Location: '.$loc_correct);
    } else {
    	header('Location: '.$loc_wrong);
    }
    exit;
    
    
    
    PHP:
     
    Scoding, Sep 26, 2012 IP
  8. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #8
    Still nothing but not found
     
    medialab, Sep 26, 2012 IP
  9. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    Did you fix the redirect URLs? I guess you edited them to not show your website.. it says website.com, not found is not because of my edits, please check
     
    Scoding, Sep 26, 2012 IP
  10. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #10
    Of course =) Im not new to websites, I just don't write PHP code well enough to fix this. Everything I put into the form field just goes to the notfound.php page.
     
    medialab, Sep 26, 2012 IP
  11. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #11
    Oh thats what you mean, I thought you just meant it as it gives you 404,

    Could you email me a sample of the file? to admin {at] scoding.com


    I will get back to you in 5 minutes, it's something to do with the line breaks, they come in all shapes and forms sometimes
     
    Scoding, Sep 26, 2012 IP
  12. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #12
    I just sent the file
     
    medialab, Sep 26, 2012 IP
  13. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #13
    I meant sample of the file with the codes, you sent me the code I posted on here :)
     
    Scoding, Sep 26, 2012 IP
  14. bm4web

    bm4web Well-Known Member

    Messages:
    718
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #14
    Last edited: Sep 26, 2012
    bm4web, Sep 26, 2012 IP
  15. MYND

    MYND Active Member

    Messages:
    136
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #15
    Scoding's snippet works fine (btw you're missing a closing bracket).

    <?php
    // Filename
    $file = "file.txt";
    // Correct Number
    $loc_correct = "http://www.website.com/success.php";
    // Wrong Number
    $loc_wrong = "http://www.website.com/notfound.php";
    
    if (isset($_POST['number'])) {
    
    	// Set Number
    	$number = $_POST['number'];
    	// Reads File
    	$file = file_get_contents($file);
    	$lines = explode("\r",$file);
    	$found = false;
    	foreach($lines as $line) {
    	    if(!$found) {
    	        if($line == $number) {
    	            $found = true;
    	        }
    	    }
    	}
    	if($found) { 
    	    header('Location: '.$loc_correct);
    	} else {
    	    header('Location: '.$loc_wrong);
    	}
    }
    ?>
    PHP:
     
    MYND, Sep 26, 2012 IP
  16. bm4web

    bm4web Well-Known Member

    Messages:
    718
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #16
    bm4web, Sep 26, 2012 IP
  17. Scoding

    Scoding Well-Known Member

    Messages:
    1,091
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    155
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #17
    Oh yeah forgot that I missed that(long night), anyhow if that one doesnt work, replace \r with \n depends if the txt file is put together with script or user.


    Anyhow should work now :)

    Edit: Got the sample file, it was '\n'
     
    Scoding, Sep 26, 2012 IP
    medialab likes this.
  18. bm4web

    bm4web Well-Known Member

    Messages:
    718
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #18
    Every thing works great here, let me know have you test the below url or not????

     
    bm4web, Sep 26, 2012 IP
  19. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    As Seller:
    100% - 2
    As Buyer:
    100% - 0
    #19
    BM4WEB - Just the error page, not working

    SCODING - The file you sent me works just fine (I had to remove a line? "haha"?) lol please send me your paypal!
     
    medialab, Sep 26, 2012 IP
  20. bm4web

    bm4web Well-Known Member

    Messages:
    718
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #20
    If you paste the below code on a php file and refresh it, then it will go every time to an not found page!

    i have rewritten the stuff here: http://seminar.b-infotech.com

     
    bm4web, Sep 26, 2012 IP