saco721 Posted November 30, 2007 Share Posted November 30, 2007 Hi!, I have the following problem with my code. I click on the 'Click to download' link and it decrements $_SESSION['total_number'] by one as expected. However, if I click the refresh button in the browser it decrements $_SESSION['total_number'] by one even though I haven't clicked on the 'Click to download' link. How do I prevent this from happening. Here is the code: <?php error_reporting(E_ALL); session_start(); if(!isset($_SESSION['total_number'])) $_SESSION['total_number'] = 3; // store session data $test1 = "myfile.pdf"; if ($_SESSION['total_number'] > 0){ echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.</a></font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("downloads complete"); } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/ Share on other sites More sharing options...
PHP_PhREEEk Posted November 30, 2007 Share Posted November 30, 2007 You could add another argument to the download link and test for it before committing to decrementing the counter. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-402993 Share on other sites More sharing options...
saco721 Posted November 30, 2007 Author Share Posted November 30, 2007 Would it be something like : <?php error_reporting(E_ALL); session_start(); if(!isset($_SESSION['total_number'])) $_SESSION['total_number'] = 3; // store session data $test1 = "myfile.pdf"; $test2 = "John Doe"; if ($_SESSION['total_number'] > 0) && ($test2 = "John Doe"){ echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.</a></font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("downloads complete"); } ?> Reason I am asking is that when I try the above, I get an error back error messsage is: Parse error: parse error, unexpected T_BOOLEAN_AND in /homepages/4/d142124103/htdocs/test_query.php Thanks Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-403015 Share on other sites More sharing options...
saco721 Posted November 30, 2007 Author Share Posted November 30, 2007 managed to remove error by using : <?php error_reporting(E_ALL); session_start(); if(!isset($_SESSION['total_number'])) $_SESSION['total_number'] = 3; // store session data $test1 = "myfile.pdf"; $test2 = "John Smith"; if ($_SESSION['total_number'] > 0 && $test2 = "John Smith"){ echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.</a></font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("downloads complete"); } ?> but now all I get is downloads complete. Please help Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-403031 Share on other sites More sharing options...
cooldude832 Posted November 30, 2007 Share Posted November 30, 2007 that or you will need to make the button be a post form or something so you can say if(!empty($_POST){ $count--; } Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-403040 Share on other sites More sharing options...
PHP_PhREEEk Posted November 30, 2007 Share Posted November 30, 2007 Your code here: <?php if ($_SESSION['total_number'] > 0 && $test2 = "John Smith"){ is flawed logic, as you are assigning the value of "John Smith" to $test2, not testing whether it contains it. Read this and thoroughly understand why, then you can move on to solving the original problem. http://www.phpfreaks.com/forums/index.php/topic,169028.msg745674.html#msg745674 PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-403214 Share on other sites More sharing options...
saco721 Posted December 4, 2007 Author Share Posted December 4, 2007 Hi PhREEEk, Thanks for the excellent tutorial!, I now have code that tests the variable to see if it contains a value rather than attempting to assign the variable with a value. code: <?php error_reporting(E_ALL); session_start(); if(!isset($_SESSION['total_number'])) $_SESSION['total_number'] = 3; // store session data $test1 = "myfile.pdf"; $test2 = "John Doe"; if ($_SESSION['total_number'] > 0 && $test2 == TRUE){ echo ("$test2"); echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.</a></font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("$test2"); echo ("downloads complete"); } ?> However it still decrements each time the browser refresh button is clicked. I now understand the fundamentals that you outlined in the tutorial, but do not understand how to add another argument to the download link and then test for it before commiting to decrementing the counter. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-406094 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 <?php error_reporting(E_ALL); session_start(); if( !isset($_SESSION['total_number']) ) { $_SESSION['total_number'] = 3; // store session data } $test1 = "myfile.pdf"; $test2 = "John Doe"; if ( $_SESSION['total_number'] > 0 && $test2 == TRUE ) { echo ("$test2"); echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.[/url]</font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("$test2"); echo ("downloads complete"); } ?> Just wanted a better format so we can clearly see the logic. Now then... there are some issues here. First off: if ( $_SESSION['total_number'] > 0 && $test2 == TRUE ) { as per my tutorial, we are just testing if $_S['total_number'] exists with a value > than 0 (and if you didn't realize it, 0 is FALSE). Immediately above, you instructed your script to do the same thing basically, which was "If it isn't set, make it so, with a value of 3" Testing for it again is redundant. An IF is a branch. You only use it if there is more than one possible outcome. In this case, at the point where that IF evaluates, $_S['total_number'] will always be greater than 0 (or TRUE). So we can cut that out... next test on the right side of the && is $test2 == TRUE. This is asking PHP to evaluate the exact same thing as it did on the left side. Understand the examples below are exact identical comparisons to what you had written: does same thing as your code: if ( $_SESSION['total_number'] == TRUE && $test2 == TRUE ) { does same thing again: if ( $_SESSION['total_number'] && $test2 ) { because remember the shortcut I mentioned... so, the left comparison isn't necessary, and we can use the shortcut on the right. Now we have this: <?php error_reporting(E_ALL); session_start(); if( !isset($_SESSION['total_number']) ) { $_SESSION['total_number'] = 3; // store session data } $test1 = "myfile.pdf"; $test2 = "John Doe"; if ( $test2 ) { echo ("$test2"); echo ("<td><font face = \"Verdana\" size = \"2\"><a href=\"samofile.php?arg1=$test1\">Click to download.[/url]</font></td>"); echo "Downloads left = ". $_SESSION['total_number'];//retrieve data $_SESSION['total_number']--; } else { echo ("$test2"); echo ("downloads complete"); } ?> We haven't changed any logic whatsoever yet, only condensed the code while removing redundant evaluations. Uh oh... we have a problem with our new slick evaluation... Right above our evaluation, we tell the script to assign a value to $test2, which is John Doe. Every single time the script runs, $test2 will be John Doe. And then we immediately test $test2 for a value, which it will always have. Redundancy strikes again... if only one condition persists, no branch is possible. The code in the middle will always execute, every time the script is refreshed. One of the things in the middle of that branch that gets executed every time is.... $_SESSION['total_number']-- = ^) Back to the drawing board with ye! Bring us some logic that will execute that middle section when it should, and not every time! PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-406184 Share on other sites More sharing options...
saco721 Posted December 4, 2007 Author Share Posted December 4, 2007 Thanks PhREEEk will potter on and try to get my logic right Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-406211 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 If you get stumped, let us know and we'll help you find the solution. I just figured once you realize why it doesn't work, you might find challenge in figuring out what will. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-406219 Share on other sites More sharing options...
saco721 Posted December 7, 2007 Author Share Posted December 7, 2007 Would I use a form instead of the href? Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-408871 Share on other sites More sharing options...
saco721 Posted December 7, 2007 Author Share Posted December 7, 2007 Could the problem be with the use of headers? Quote Link to comment https://forums.phpfreaks.com/topic/79565-solved-counter-decrements-on-browser-refresh/#findComment-408963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.