| ||||||||||||||||
| ||||||||||||||||
Reading file contents Posted by lostme (lostme), 10 July 2005 ![]() i hope some one can help me with this I've tryied a few forums but i cant get a simple answer here is what im tryin to do.. I have a file called 'data.txt' which has urls stored in it. each url is seperated by a '|' . All i want to do is open the data.txt file and check if a entered url exsists in the file example: $ref = getenv("HTTP_REFERER"); $urls = explode("|", file_get_contents("data.txt")); foreach($urls as $key => $dataurls) { if ($ref == $dataurls) { // Do something here } this code above only checks the first url in the data file and i want it to check all the urls in the file to see if the $ref exists im a total noob so please explain any suggestions I have been told to try preg_match or strpos i've looked into both of these at php.net but dont know how to incorp this to my script both seem to complicated for such a simple task i need to create a loop but dont know how please help many thanks in advance Posted by admin (Graham Ellis), 11 July 2005 Are you looking to maintain a list of referring URLs to your pages in a text file, and then check eachincoming request to see if it's from a know referer or from somewhere new? If that's the case, I can put up a snippet of code to do the trick first thing tomorrow!Posted by lostme (lostme), 11 July 2005 hi thanks for the replyi want to save any url from a form This bit i have done the problem is checking the referal url against saved onces in a data file if that makes sence thanks again for your help ![]() Posted by admin (Graham Ellis), 12 July 2005 I'm still having a bit of difficulty fully following the question, so I've written a little demo in PHP that logs referers in a text file. For each visitor to the page, the referer is checked against a known file of referers and is either counted, or a new line is added to the file with a count of 1. I've tested this and it works; the code is relatively straightforward so should be quite easy to follow.Code:
My data file after testing looked like ... Code:
Notes ... 1. Be aware of security / access rights issues on the data file. I would suggest you keep it in a different directory. 2. On a very busy sight, you may get corruption of the data file if several people are accessing it in the same instance. The solution would be to switch from a data file to a database table. This would also make it more efficient if there were lots of different referers Please feel free to adopt / adapt my code; if you're so new to PHP that it's not easy for you to do so, then you would be well advised to fill in your background from a book, descriptive websites, or appropriate training ... then come back to the forum with any more specific questions you have. Posted by lostme (lostme), 12 July 2005 hi thanks for that but its not what im afterok check this out - its like an anti leech script just to demonstrate what i mean... Code:
now thats all out the way heres the problem the script only checks the 1st URL in the data.txt file example of inside of date.txt in order ------------------------------------------------- http://www.mysite.com/location/1.html //address 1 http://www.mysite.com/location/2.html //address 1 -------------------------------------------------- so if you clicked the link from address 1 the file will be downloaded because its the first in the list but address 2 will not work because it wont read past the first address (1) i have tried changing foreach($urls as $url) to foreach($urls as $val =>$url) but its exactly the same i tried also if (in_array..... this works to some extent but if you copy the short-cut of the link in any of the pages and paste it in browser it downloads it which defeats the whole purpose lol hope this has been easy to read & understand thanks Posted by admin (Graham Ellis), 12 July 2005 You might find it easier to use the file function to read a file line by line into an array rather that your more complex combination of explode and file_get_contents. I'm not understanding though how the \n separator in this post relates to the | separator in your earlier post? I think it would be a good idea to print out what you have loaded into $urls to check that you have it properly set up for the loop ...Now - I suspect that your main problem in the latest code is a logic error. You have your script exiting as soon as it finds a URL that does NOT match - i.e. I now understand that you're looking to allow links in ONLY from certain pages. What you want to do is to set a variable (let's call it $goodurl) to 0 at the top of your code, then set it to 1 within the foreach loop if the URL matches. Remove the exit from within the foreach loop, and add an extra piece of code at after the loop if ($goodurl == 0) { error handling stuff and exit} and I think that should correct your logic. Posted by lostme (lostme), 12 July 2005 well i did as you said ![]() getting error Parse error: parse error, unexpected $ in /.......php on line 41 i cant fix it Code:
Posted by lostme (lostme), 12 July 2005 oh yeah forgot to sayto link to a file 'script.php?file=[filename.ext] im think this might be the problem maybe i should try 'script.php?action=get&&file=[filename.ext] what you think ![]() hmm ![]() Posted by admin (Graham Ellis), 12 July 2005 Your problem is a syntax error - nothing to do with the calling URL - and it's on (or shortly before) line 41 of your script.As the script you've posted in only 38 lines long, I thing you've only posted a part of it - that's fine but it leaves me guessing which line is being complained about. Call up your full script in your editor and have a careful look at line numbers 41 and 40! Posted by lostme (lostme), 12 July 2005 $filesdir = 'files'; $urlfolder = 'allowed'; $from = getenv("HTTP_REFERER"); $addurl = trim($_POST['addurl']); $dlfile = $_GET['file']; $file = "./$filesdir/$dlfile"; $goodurl = 0; if (isset ($dlfile)) { $urls = file("data.txt"); foreach($urls as $url) { if ($from == $url) { $goodurl = 1; } include ("leeched.php"); } if ($goodurl == 1) { if (file_exists($file)) { $fp = fopen($file, "rb"); << is the line in question header("Content-Length: " . filesize($file)); header("Content-Disposition: attachment; filename=" . $dlfile); fpassthru($fp); } else { echo "Error! File $dlfile does not exist"; exit; } sorry i cant figure it out thanks ![]() Posted by admin (Graham Ellis), 12 July 2005 That "if" block compiled / ran for me ...1. I notice that your { and } don't balance at the end - but that could be because you've simply copied and pasted the relevant snippet of code 2. I also note that you have carfully deleted the file name from the error message. Is it possible that the error was at line 41 of the included leeched.php file rather than in the code you quoted? If neither of these solves the problem, start adding in / taking out blank lines around line 41 of the code and see when the "41" changes to "42" ... that way, you'll tie it down exactly to a single line. Also worth calling your code up with an editior such as vi in "list" mode, or looking at it through the od utility, so that you can check for non-printable control characters that may have been edited in - these sometimes cause odd results! Posted by lostme (lostme), 12 July 2005 Quote:
sorry which part of the code do you mean Posted by admin (Graham Ellis), 12 July 2005 There is no } for the if ($goodurl == 1) { block.There is no } for the if (isset ($dlfile)) { block either. Use an editor such as vi (the % key) or a context sensitive editor to check your brackets ![]() Posted by lostme (lostme), 12 July 2005 lol i dont understand the } { i get a bit lost in them ill try and sort it thanks Posted by lostme (lostme), 12 July 2005 im using dreamweaver mxcan you point me in the right direction for such a groovy program lol if (isset ($dlfile)) { $urls = file("data.txt"); foreach($urls as $url) { if ($from == $url) { $goodurl = 1; } include ("leeched.php"); } } if ($goodurl == 1) { if (file_exists($file)) { $fp = fopen($file, "rb"); header("Content-Length: " . filesize($file)); header("Content-Disposition: attachment; filename=" . $dlfile); fpassthru($fp); } else { echo "Error! File $dlfile does not exist"; exit; } } is that any better - like i know (not!) Posted by admin (Graham Ellis), 12 July 2005 Quote:
Yes - that looks fine. The { means "start" and the } means "end" and they must balance. I'm not sure how to 'jump blocks' in Dreamweaver - I'm sure it can do it but it's not my regular tool. Posted by lostme (lostme), 12 July 2005 i think you mean this but it still wont workgot no error messages this time it wont allow the file to be downloaded at all lol jumps straight to error message in script Code:
this would be so much easier if each saved url was a file name rather then in a file if you know what i mean ![]() Posted by admin (Graham Ellis), 13 July 2005 Does the file exist? Do you have read permissions for it for the web server? Are you in the correct directory for it / do you have the correct path?This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| |||||||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |