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.

Small coding issue? Need Help Please!

Discussion in 'PHP' started by medialab, Jul 22, 2012.

  1. #1
    Hey Everyone,

    I have a small PHP script that reads a text file for a set of numbers (ie. 74836 or A37484) if the number exists in the text file it goes to page A, if it does not page B. It works great, however if a letter gets entered first (we have letters as well in the text file as above) it automatically goes to Page A even though the combination is not in the text file, can anyone tell me what part of this code to change to make sure it matches (including letters) to go to page A instead of B? The code is entered in a form field POST.

    Thanks!!

    
    <?
    
    	// Filename
    	$file = "file.txt";
    	// Correct Number
    	$loc_correct = "http://www.websitename.com/Page_A.php";
    	// Wrong Number
    	$loc_wrong = "http://www.websitename.com/Page_B.php";
    	
    	if (isset($_POST['number'])) {
    	
    		// Set Number
    		$number = $_POST['number'];
    	
    		// Open File
    		$file = fopen($file,"r") or exit("Unable to open file!");
    			
    		// Run Through File
    		$found = false;
    		while(!feof($file)) {
    			if ($number == (int)fgets($file)) {
    				header('Location: '.$loc_correct);
    				exit;
    			}
    		}
    		
    		header('Location: '.$loc_wrong);
    		exit;
    	
    	}
    
    ?>
    
    Code (markup):

    SEMrush
     
    Solved! View solution.
    medialab, Jul 22, 2012 IP
    SEMrush
  2. jeet25

    jeet25 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think u can change this part

    if ($number == (int)fgets($file)) , u can use regular expression to make the match
     
    jeet25, Jul 22, 2012 IP
  3. #3
    Hi,
    Try this script:
    <?php
    // Filename
    $file = "file.txt";
    // Correct Number
    $loc_correct = "http://www.websitename.com/Page_A.php";
    // Wrong Number
    $loc_wrong = "http://www.websitename.com/Page_B.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):
     
    MarPlo, Jul 22, 2012 IP
    medialab likes this.
  4. medialab

    medialab Well-Known Member

    Messages:
    362
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #4
    Thanks a million! Worked like a charm!!
     
    medialab, Jul 22, 2012 IP