Jump to content

[SOLVED] counter decrements on browser refresh


saco721

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.