Languages :: PHP :: counter in php |
|||
| By: irish_paddy |
Date: 22/04/2003 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
Hello Is there anyway I can set up a counter in php so that it initally is set to zero in a php webpage and then is incremented by one each time that webpage is opened up. I want to then reset the counter when the user clicks on a specified button. Cheers |
|||
| By: VGR | Date: 22/04/2003 21:29:00 | Type : Comment |
|
| yes. Use a session variable. This way, only the "surveyed" page has to have session_start(); at the beginning, the others don't need it. something like : <?php session_start(); if (action_button) $_SESSION['sess_counter']=0; // write the action the usual way for you, like $_POST['clear'] where 'clear' would be the name of a type=submit button in a method=post FORM... // else { ? // that's if you want to start over at 0 really, or 1 (because you are on the page again, after all) $_SESSION['sess_counter']++; // } // has to do with the else above ?> |
|||
| By: ThG | Date: 22/04/2003 21:31:00 | Type : Comment |
|
| do you have any database support? you may do it with to queries: "update set hits=hits+1 where page_id=$my_page_id" and "update set hits=0 where page_id=$my_page_id" otherwise you should use a text file containing only the number: function increment_counter() { $fd = fopen("count.txt", "r"); $count = fgets($fd); $count = $count + 1; fclose($fd); $fd = fopen("count.txt", "w"); fputs($fd, $count); fclose($fd); } |
|||
| By: ThG | Date: 22/04/2003 21:32:00 | Type : Comment |
|
| damn, VGR arrived first :) but my solution is better than his, especially if the counter has to be global for all users opening that page, and not for only that user.. |
|||
| By: irish_paddy | Date: 22/04/2003 21:39:00 | Type : Comment |
|
| Cheers, just somethin on security im just after discoverin, its completley unrelated. Do you know how I can hide certain pages from users depending on what value I assign them in a "access" row in a mysql database. I'll increase the points. Thanks |
|||
| By: ThG | Date: 22/04/2003 21:45:00 | Type : Comment |
|
hmm i would suggest something like this: $min_level = 5; // minimum level to access this page. include("check_level.inc.php"); inside that file: //query your database and store the result in $user_level if ($user_level < $min_level) { print "<h2>you don't have access to this page</h2>\n"; } |
|||
| By: VGR | Date: 22/04/2003 21:46:00 | Type : Comment |
|
| of course you can. -in the "users" table, you set up "levels" -when the user connects (login), you recover those values from the DB and set up session variables for them -on entry of each page, you check the session variables for that user (you for sure have his pseudo/login, his opreferences for display, now you'll have his "level") -and you write : <?php $threshold=3; // example, this varies with the page if ($_SESSION['userlevel']<$threshold) Header("Location: index.php"); // thrown out // else continue and display the page... ?> |
|||
| By: irish_paddy | Date: 22/04/2003 21:50:00 | Type : Comment |
|
| ok, ill try that. On the previous question, Can I put the count value in a php variable. $count = $_SESSION['sess_counter']++; Would this work? |
|||
| By: VGR | Date: 22/04/2003 21:55:00 | Type : Comment |
|
| yes, of course then you would use the (incremented) variable $count in the page , right ? it will be easier to display than $_SESSION['sess_counter'] ;-) |
|||
| By: VGR | Date: 22/04/2003 21:55:00 | Type : Comment |
|
| sorry, but you'll have the unincremented value doing this. |
|||
| By: irish_paddy | Date: 22/04/2003 21:59:00 | Type : Comment |
|
| ok, see im creating a unique table in a mysql database everytime the suer clicks a certain button. wil the query "create table tablename$_SESSION['sess_counter']++" work? |
|||
| By: ThG | Date: 22/04/2003 22:06:00 | Type : Comment |
|
| yes... provided that you fix the syntax to "create table tablename" . $_SESSION['sess_counter']++ . " (other data..);" |
|||
| By: VGR | Date: 22/04/2003 22:09:00 | Type : Comment |
|
| it's definitely better to separate things... $_SESSION['sess_counter']++; $counter=$_SESSION['sess_counter']; "create table tablename$counter;" |
|||
| By: irish_paddy | Date: 23/04/2003 21:43:00 | Type : Comment |
|
| I was trying that, Is this the correct way to link the button to the php file? <center> <FORM METHOD="LINK" name = form ACTION="new.htm"> <INPUT TYPE="submit" VALUE="Create New Timetable" onClick="javascript:sendform()"> </center> </FORM> session_start(); if(form) $_SESSION['sess_counter']=0; { $_SESSION['sess_counter']++; } |
|||
| By: VGR | Date: 23/04/2003 22:47:00 | Type : Answer |
|
| yes and no you could as well do it the "normal way" : ---- in the page with the buttons (they may be the same "new.htm" ) <center> <FORM METHOD=POST ACTION=new.htm> <INPUT TYPE=submit NAME=form VALUE="Create New Timetable"> </FORM> </center> ------- in new.htm now : <?php session_start(); if ($_POST['form']) $_SESSION['sess_counter']=0; else $_SESSION['sess_counter']++; $locCounter=$_SESSION['sess_counter']; // for local use ?> here HTML output with value of the counter = <? echo $locCounter;?> |
|||
| By: irish_paddy | Date: 24/04/2003 21:54:00 | Type : Comment |
|
| Is there any way I can increment the counter and load another html page by pressing the same button once? Again cheers. |
|||
| By: VGR | Date: 24/04/2003 22:03:00 | Type : Comment |
|
| Of course there is, but for me to answer, I need to better understand what exactly you're trying to do ;-) 1) how many pages ? 2) where is this button ? 3) how to select the next page ? 4) relation between counter and other page ? the way it was asked, the counter is linked to a specific page and is reset when the client presses "the button" |
|||
| By: irish_paddy | Date: 24/04/2003 22:14:00 | Type : Comment |
|
| ok, heres what im trying to do When the user clicks on a "create" button, the counter is incremented in a different php file and a tablename with the counter number is made in the database in that same php file. Clicking the "create" button also links the user to a different ".htm" page. Thats it really... |
|||
| By: VGR | Date: 24/04/2003 22:27:00 | Type : Comment |
|
| not understood php files : "a diferent" + "the same" I also thought that the "create" button reset the counter to zero "a counter is incremented..." : a counter of what ? and towards which page is the user supposed to be redirected ? Always the same page ? |
|||
| By: irish_paddy | Date: 24/04/2003 22:32:00 | Type : Comment |
|
| I am fairly bad at explainin what I wanna do. When the user clicks the "create" button below the php code is executed which is in a different "php" file. I just want to link the user to another "htm" page while at the same time executing the "php" file. Is that any clearer? <center> <FORM METHOD=POST ACTION=new.htm> <INPUT TYPE=submit NAME=form VALUE="Create"> </FORM> </center> <?php session_start(); if ($_POST['form']) $_SESSION['sess_counter']=0; else $_SESSION['sess_counter']++; $locCounter=$_SESSION['sess_counter']; // for local use ?> here HTML output with value of the counter = <? echo $locCounter;?> |
|||
| By: VGR | Date: 24/04/2003 23:21:00 | Type : Comment |
|
| you can't but you can :D 1) add a hidden field "desturl" in the FORM and in the php script, test $_POST['desturl'] and do : Header('Location: '.$_POST['desturl']); 2) the same, but with the desturl in the action= part of the form, like : <FORM METHOD=POST ACTION=new.htm?desturl=...> Then treat it as $_GET['desturl'] in the receiving php script |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








