Link to home
Create AccountLog in
Avatar of Djrobluv
Djrobluv

asked on

Simple PHP Query a Text file for value

I have been given a project that would require a user entering a Passcode on a Web page that I need to have PHP read a text file to see if the value exists in the text file. If the value exists redirect the user to a page that contains that passcode in the URL. The website was done in PHP and I'm not all that familiar with it PHP. I was told that I could probably get someone to provide me with some sample code I could tweak to get this to work. So if anyone could help me out with this I would greatly appreciate it.
Avatar of iGottZ
iGottZ
Flag of Germany image

Sounds a bit "homework", still, something like this (UNTESTED)

<?php


  function checkFile( $filename, $string ) {

    $text = file_get_contents( $filename );
    
    return ( strpos( $text, $string ) !== false );

  }


   ... more program code....

  if ( checkFile( "/www/htdocs/afile.txt", "password" ) ) { 
     header("Location: http://example.com?password=whatever");
     exit;
  }

Open in new window


http://uk3.php.net/file_get_contents
Please post your test data, the current PHP scripts that you need to integrate this into, and a link to where they currently run.  It should be pretty easy to give you an exact solution once we see the test data set.
Avatar of Djrobluv
Djrobluv

ASKER

I'm still doing the Pseudo code on paper. No test data setup yet. I want to use a Txt file that contains the passcodes, have a user go to a webpage to enter the passcode and have Php read the TXT file to see if the value exists and then redirect them to a URL that contains that passcode as part of the URL if it exists. For example. passcode = 12345 URL = 12345.html. I will be using a HTML Form to pass the passcode parameter to the PHP process.
Should be able to do it with a minor change to bportlock's example.

Just curious about the bigger picture... Why are you doing this?  If you give us a 50,000 foot view of the problem we may know a potentially better design pattern.
A client of mine wants to give his external clients access to Excel spreadsheets via his website. He wants them to enter a passcode that would link them to a page that contains links to the excel spreadsheets so they can download them.
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sorry it took me so long to get back to you but I had to go out of state. I was able to use the sample code from above to put together a solution that will be sufficient.