/    Sign up×
Community /Pin to ProfileBookmark

how do I write to file

I would like to use this script Kahlid let me copy, and write a php file within it, so that when the number goes up with the click of the button, and you leave, when you come back the same amount (unless it went up) will remain on the page. I have no PHP exp. and I would like something to mess with, so if you wouldnt mind hooking me up with a script for this, I would greatly appreciate it.

Thanks

Code One

[code]
<script type=”text/javascript”>
/**************************************************************************************
Author : Khalid Ali
Date : July 30,2003
Version : 1.1
Company : webapplikations inc. [url]http://www.webapplikations.com[/url]
The code provided here is copy righted material and belongs to the
webapplikations.com inc. It is provided as a service for our visitors.
It can be used without permission provided that this notification
does appear at the top.
Description :
***************************************************************************************/
<!–
//get the command for the starting point in menu if any
var cmdStr = window.location.search;
cmdStr = (cmdStr.indexOf(“menu_id”)>-1)?cmdStr.substring(cmdStr.indexOf(“=”)+1,cmdStr.length):””;

var isNS6 = (navigator.userAgent.indexOf(“Mozilla”)>-1 && navigator.userAgent.indexOf(“Gecko”)>-1)?true:false;
var isIE = (navigator.userAgent.indexOf(“MSIE”)>-1 && navigator.userAgent.indexOf(“Opera”)==-1)?true:false;
var isOp = (navigator.userAgent.indexOf(“Opera”)>-1)?true:false;

function init(){
if(isIE){
document.getElementById(“msg_scroller”).style.position=”relative”;
}
}

var limit = 5;
var clickCtr=0;
function CountClicks(val){
var frm = document.getElementById(“form1″);
var len = frm.length;
var num = parseInt(val);
clickCtr = clickCtr + num;;
if(clickCtr>limit){
clickCtr = clickCtr-1;
}else if(clickCtr<0){
clickCtr++;
}
frm.t1.value = clickCtr;
}
//–>
</script>

</head>

<body class=”body”>

<div class=”maindiv”>

<div id=”div_1″ class=”table-with-thin-border” style=”margin:auto;width:140px;background-color:#FFFFFF;”>
<form id=”form1″ action=”” onsubmit=””>
<p>
<span style=”display:block;text-align:left;”>
<label class=”small-text-bold”>
Click Counter :<input type=”text” name=”t1″ value=”0″ style=”width:24px;” maxlength=”2″ readonly=”readonly”/>
</label>
</span>
<span style=”display:block;text-align:right;margin-top:-20px;”>
<span style=”display:block;padding-bottom:4px;”>
<img src=”../images/up.gif” width=”12″ height=”16″ alt=”” onclick=”CountClicks(1);”/>
</span>

</p>
</form>

</div>
</div>
</body>
</html>
[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@deadbeefNov 17.2003 — I don't really understand what you want. If you wanna keep some info on client's machine - the only way to do that is a cookie, you can not write any files anywhere exept server itself.
Copy linkTweet thisAlerts:
@pyroNov 17.2003 — Yes, do you want the counter to be user specific, or count all the hits to your page?
Copy linkTweet thisAlerts:
@Code_OneauthorNov 17.2003 — id like the counter to be included in a play button I have already in the html doc. So when the user clicks play on a song, the count script will keep track of how many times the play button was pressed. I already have everything but the count script working properly. I guess all I need is some PHP help, on keeping track of the counters present amount. You know what Im saying...Id certainly need a server side script, that would allow me to write data to it. Imagine this, you come to a page with music on it, and there are 5 songs to choose from. And what I want is for when someone chooses to listen to one of the songs, and clicks play, the counter will record the click, and display it's current sum. So if it has been listened to 5 times and someone comes along and clicks play again, it would then read 6. And the PHP that makes it all possible is what I need. So that when people leave and come back the display would still show 6, and so on, for each song. I hope this explains me better.

I appreciate the help

Code One
Copy linkTweet thisAlerts:
@pyroNov 17.2003 — Here's the code for a simple counter that should be easy enough to modify to do what you need:

[code=php]<?PHP
$file = "counter.txt"; #chmod to 666
$contents = @file($file); #open the file for reading
$count = $contents[0]; #put the first line of $contents into $count
$count++; #increment $count by 1
$fp = fopen($file, "w"); #open the file for writing.
flock ($fp, 2); #lock the file
fwrite ($fp, $count); #write count to the file
flock($fp, 3); #release the file
fclose ($fp); #close the file
echo $count;
?>[/code]
Copy linkTweet thisAlerts:
@Code_OneauthorNov 17.2003 — <i>
</i>&lt;script type="text/javascript"&gt;
/ **************************************************
************************************
Author : Khalid Ali
Date : July 30,2003
Version : 1.1
Company : webapplikations inc. <a href="http://www.webapplikations.com">http://www.webapplikations.com</a>
The code provided here is copy righted material and belongs to the
webapplikations.com inc. It is provided as a service for our visitors.
It can be used without permission provided that this notification
does appear at the top.
Description :
**************************************************
*************************************/
&lt;!--
//get the command for the starting point in menu if any
var cmdStr = window.location.search;
cmdStr = (cmdStr.indexOf("menu_id")&gt;-1)?cmdStr.substring(cmdStr.indexOf("=")+1,cmdStr.length):"";


var isNS6 = (navigator.userAgent.indexOf("Mozilla")&gt;-1 &amp;&amp; navigator.userAgent.indexOf("Gecko")&gt;-1)?true:false;
var isIE = (navigator.userAgent.indexOf("MSIE")&gt;-1 &amp;&amp; navigator.userAgent.indexOf("Opera")==-1)?true:false;
var isOp = (navigator.userAgent.indexOf("Opera")&gt;-1)?true:false;


function init(){
if(isIE){
document.getElementById("msg_scroller").style.position="relative";
}
}

var limit = 5;
var clickCtr=0;
function CountClicks(val){
var frm = document.getElementById("form1");
var len = frm.length;
var num = parseInt(val);
clickCtr = clickCtr + num;;
if(clickCtr&gt;limit){
clickCtr = clickCtr-1;
}else if(clickCtr&lt;0){
clickCtr++;
}
frm.t1.value = clickCtr;
}
//--&gt;
&lt;/script&gt;

&lt;/head&gt;

&lt;body class="body"&gt;
&lt;?PHP
$file = "counter.txt"; #chmod to 666
$contents = @file($file); #open the file for reading
$count = $contents[0]; #put the first line of $contents into $count
$count++; #increment $count by 1
$fp = fopen($file, "w"); #open the file for writing.
flock ($fp, 2); #lock the file
fwrite ($fp, $count); #write count to the file
flock($fp, 3); #release the file
fclose ($fp); #close the file
echo $count;
?&gt;

&lt;div class="maindiv"&gt;


<i> </i>&lt;div id="div_1" class="table-with-thin-border" style="margin:auto;width:140px;background-color:#FFFFFF;"&gt;
<i> </i>&lt;form id="form1" action="" onsubmit=""&gt;
<i> </i>&lt;p&gt;
<i> </i> &lt;span style="display:block;text-align:left;"&gt;
<i> </i> &lt;label class="small-text-bold"&gt;
<i> </i> Click Counter :&lt;input type="text" name="t1" value="0" style="width:24px;" maxlength="2" readonly="readonly"/&gt;
<i> </i> &lt;/label&gt;
<i> </i> &lt;/span&gt;
<i> </i> &lt;span style="display:block;text-align:right;margin-top:-20px;"&gt;
<i> </i> &lt;span style="display:block;padding-bottom:4px;"&gt;
<i> </i> &lt;img src="../images/up.gif" width="12" height="16" alt="" onclick="CountClicks(1);"/&gt;
<i> </i> &lt;/span&gt;


<i> </i>&lt;/p&gt;
<i> </i>&lt;/form&gt;





&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


Look how i put the php code in to my script is that right? Or does it go in the head? What should i name this file, does it matter? And do I need to make a blank text file and name counter.txt and upload it to server?

thanks

code one
Copy linkTweet thisAlerts:
@Code_OneauthorNov 17.2003 — i cant get it to save my counts. I rendered the file as count.php, and loaded up to the server, and I made a blank, txt file, and named it counter.txt, and uploaded to the server, and I tested the script and it doesnt keep my data. What am I doing wrong?????

help,

Code One
Copy linkTweet thisAlerts:
@pyroNov 17.2003 — Try just getting it to run without any of the rest of your code, first.

Did you remember to CHMOD the .txt file, and is it in the same directory as the .php file? Are you getting any errors?
Copy linkTweet thisAlerts:
@Code_OneauthorNov 17.2003 — about as green as MT.dew...to PHP.. I ddidn't know I had to CHMOD the txt file, how do I do that if it isn't to difficult to post. So I dont need to put that txt file in a cgi-bin folder? Alright so keep it in the same directory, got it. But if i justput the php in by itslef, how would i know if it's working? Im confused, pyro.

Code One

I bet when I get this finally, it should all start to flow, easily, please be paitient. ?

edit: Alright I took a look at an article and I suppose I would want to use chmod 666 so all users can read and write to the file, which would be what i want right? I want to chmod using 666 right? Ok lets say thats it...now how do i go about chmod the file, do I name the text file( 666 counter *.txt ), something like that?

CO
Copy linkTweet thisAlerts:
@deadbeefNov 18.2003 — In any ftp client it is very easy to do - just highlight the file and look for the appropriate command in menu =) Usually it is something kinda "attributes". They should be set as 666 or rw-rw-rw-

P.S. Or you can include this code in your script =):

chmod ($file, 0666);

eg:

<?PHP

$file = "counter.txt"; #chmod to 666

chmod ($file, 0666);

$contents = @file($file); #open the file for reading

$count = $contents[0]; #put the first line of $contents into $count

$count++; #increment $count by 1

$fp = fopen($file, "w"); #open the file for writing.

flock ($fp, 2); #lock the file

fwrite ($fp, $count); #write count to the file

flock($fp, 3); #release the file

fclose ($fp); #close the file

echo $count;

?>
Copy linkTweet thisAlerts:
@deadbeefNov 18.2003 — Actually including this command will make the execution of the script a little bit longer, so when everything will start working OK remove that line
×

Success!

Help @Code_One spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...