herme3 Posted June 18, 2006 Posted June 18, 2006 I don't know very much about programming, so I need a little help with something I'm working on. I want to upload a text file on a server that will contain a number. Whenever a certain web page is loaded, a code on that page will increase that number by one. If different page is loaded, a code on the different page will subtract that number by one. I also want an IF...Then code to put on a page so a certain action will happen if the number is greater than 0. If the number is greater than 0, the page will redirect to a certain URL, or if the number is 0 the page will redirect to a different URL. I hope I was able to explain what I want to do without making it sound too confusing. Can anybody please help me? Thanks.
Cap'n Refsmmat Posted June 18, 2006 Posted June 18, 2006 What sort of language can you use for it? PHP? ASP?
mooeypoo Posted June 18, 2006 Posted June 18, 2006 Well, I am a php person, so here's something quick with php. On the page you want to update the text-file count, you put this code (wherever): <?php //open the file for reading $ReadFile=fopen("filename.txt","r") or die; //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); fclose("filename.txt"); //open the file for writing: $WriteFile= fopen("filename.txt","w") or die; fwrite($WriteFile,$CurrentNumber); fclose("filename.txt"); ?> NOW, you can make whatever IF/ELSE condition you wish using this "CurrentNumber" variable... I didnt test it, but it should work fine. also, I wrote this under the assumption that your filename.txt is under the same folder as your code. ~moo
herme3 Posted June 18, 2006 Author Posted June 18, 2006 I know the server has PHP support, so that would be good. I will just need to have a URL that has a code that would increase a number in a text file. I will also need a different URL that would decrease the same number. I also need a separate URL that will check the number, and then redirect to an HTML page if the number is greater than 0, or redirect to another page if the number is 0. Edit: Ok, thanks Moo. I didn't see your post when I first made this post, but I'll try out your code and see if it works.
mooeypoo Posted June 18, 2006 Posted June 18, 2006 I just noticed I didnt increase (or decrease) the numbers.. oops... here's a correction: <?php //open the file for reading $ReadFile=fopen("filename.txt","r") or die; //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); fclose("filename.txt"); //change the number: $IncreasedNumber = $CurrentNumber + 1; $DecreasedNumber = $CurrentNumber - 1; //open the file for writing: $WriteFile= fopen("filename.txt","w") or die; fwrite($WriteFile,$IncreasedNumber); fclose("filename.txt"); ?> I don't really understand what you want done.. this code I gave here takes a number off a textpage, increases (or decreases it) and pastes the NEW number on the same textpage. Maybe explain your purpose of this code? I might be able to find other ways to help out... ~moo
mooeypoo Posted June 18, 2006 Posted June 18, 2006 about your if/else condition. two ways depending what you want to do. First method is to act if the number is zero, or not zero: <?php //open the file for reading $ReadFile=fopen("filename.txt","r") or die; //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); //the condition (FIRST method): if ($CurrentNumber == 0) { print "It's zero!"; } else { print "It's NOT zero!"; } fclose("filename.txt"); ?> the other thing you can do, is act by specific numbers: <?php //open the file for reading $ReadFile=fopen("filename.txt","r") or die; //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); //the condition (SECOND method): switch ($CurrentNumber) { case 1: print "Equals 1!"; break; case 2: print "Equals 2!"; break; default: print "It isnt 1, nor is it 2, but rather, it is ".$CurrentNumber; breaks; } fclose("filename.txt"); ?> Just out the top of my head.. but try explaining what this is for maybe? I might be able to help more effectively. ~moo
herme3 Posted June 18, 2006 Author Posted June 18, 2006 Sorry, maybe it would have been more helpful if I explained more in my first post. It will be like a banner exchange. The text file will be used to keep track of how many credits a user has. When somebody displays another banner on their site, it will add a credit to their account. When their banner is displayed on another web site, it will subtract a credit from their account. I will have it setup with a rotator. If it rotates to a certain banner, it will subtract a credit from that account and then redirect to the banner. If it rotates to a banner without any credits, it will just keep going back to the rotator URL until it finds an account with credits.
mooeypoo Posted June 18, 2006 Posted June 18, 2006 Hm. Okay, I'd personally do it through a database, but textfiles work too if you insist So basically, if I understood what you're planning, you need a routine that adds or substracts from a number in a textfile. What you can do, is have an external file with the code I will give you, then, in each page you want this routine to work, you write this: <?php include("addsubroutine.php"); AddSubstract("textfile.txt","Add"); //or replace 'add' with 'substract' ?> The code to be written in the new addsubroutine.php page is this: <?php function AddSubstract($fileName,$FuncToDo) { //open the file for reading $ReadFile=fopen($fileName,"r") or die("No Such File"); //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); fclose($fileName); switch ($FuncToDo) { case "Add": $NewNum = $CurrentNumber + 1; break; case "Substract": $NewNum = $CurrentNumber - 1; break; } $WriteFile= fopen($fileName,"w") or die; fwrite($WriteFile,$NewNum); fclose($fileName); } ?> Now, you have your 'substract' / 'add' credits script. Assuming you have different files for different people, you'll have to go through each file to decide which of them has enough credits to show in the rotator you want to do. Is that correct? Just let me know if I'm in the right track.. ~moo
herme3 Posted June 18, 2006 Author Posted June 18, 2006 Ok, I think I got it. All I need now is a redirect code to add to the PHP files. How can I get it to redirect to another URL?
mooeypoo Posted June 18, 2006 Posted June 18, 2006 this is a redirection procedure through PHP: <?php header("Location: whateverpage.php"); exit; ?> OR <?php header("Location: http://www.cnn.com"); exit; ?> You should put it either on an EMPTY page, or above all the HTML code -- it won't redirect if the page already has "outputs" (text on it). ~moo
herme3 Posted June 18, 2006 Author Posted June 18, 2006 Thanks, Moo. I got everything working except for the redirect code. Is there any other redirect code I could use within the PHP code? There will be other code on the page too, and I want the redirect code to be within an If...Then statement.
mooeypoo Posted June 18, 2006 Posted June 18, 2006 CREATE A NEW EMPTY FILE Name: addsubroutine.php In it, put this: <?php function ExtractNumber($fileName) { //open the file for reading $ReadFile=fopen($fileName,"r") or die("No Such File"); //read the first line (number): $CurrentNumber = fgets($ReadFile,1024); return $CurrentNumber; fclose($fileName); } function AddSubstract($fileName,$FuncToDo) { $CurrentNumber = ExtractNumber($fileName); switch ($FuncToDo) { case "Add": $NewNum = $CurrentNumber + 1; break; case "Substract": $NewNum = $CurrentNumber - 1; break; } $WriteFile= fopen($fileName,"w") or die; fwrite($WriteFile,$NewNum); fclose($fileName); } ?> In your normal HTML code, BEFORE the <html> tag and at the top of the page, add this: <?php include("addsubroutine.php"); if (ExtractNumber("whoever.txt")>0) { //stay on this page, since it's higher than zero //and add to the number: AddSubstract("whoever.txt","Add"); } else { //the counter shows zero, so redirect to a different page: header("Location: otherpage.php"); exit; } ?> That should do it, if I understood what you wanted to do correctly ~moo
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now