Jump to content

session problem?


marcus

Recommended Posts

I'm trying to make a session-based "scratch card" type thing.

 

I'm allowing 6 out of 9 slots to be scratched, and when the scratch amount reaches 6 no more links should appear.

 

My problem is when there actually is six scratched the other three are shown as links, and they should be shown as text.

 

http://img406.imageshack.us/img406/2688/scre94389lz6.jpg

 

code

 

<?php
session_start();


$prizes = array('10000', '5000', '2500', '1000', 'blank', 'item', 'blank', '500',
    'blank', 'blank');

if (!$_SESSION['clicked']) {
    $_SESSION['clicked'] = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
    $_SESSION['prizes'] = array();

    foreach ($_SESSION['clicked'] as $clicked) {

        $_SESSION['prizes'][] = array_rand($prizes);
    }
}
$clickz = $_GET['click'];

if ($clickz) {
    if (in_array($clickz, range(1, 9))) {
        $_SESSION['clicked'][$clickz - 1] = "clicked_" . $_SESSION['prizes'][$clickz - 1];
        header("Location: card.php");
    }
}

$go = $_GET['go'];
if ($go) {
    session_destroy();
    header("Location: card.php");
}


echo "<table border=\"1\" cellpadding=\"3\" cellpadding=\"3\" width=\"20%\">\n";
echo "<tr>\n";
$x = 1;
$_SESSION['y'] = 0;

foreach ($_SESSION['clicked'] as $click) {


    $ex = explode("_", $click);
    if (count($ex) > 1) {
        $echo = $prizes[$ex[1]];
        $_SESSION['y']++;
    } else {

        if ($_SESSION['y'] >= 6) {
            $echo = $_SESSION['prizes'][$click - 1];
        } else {
            $echo = "<a href=\"card.php?click=" . $click . "\">" . $_SESSION['prizes'][$click - 1] . "</a>";
        }
        
    }


    echo "<td align=\"center\" valign=\"middle\">" . $echo . "</td>\n";

    if ($x == 3) {
        echo "</tr><tr>\n";
        $x = 0;
    }

    $x++;
}
echo "</tr>\n";
if ($_SESSION['y'] >= 6) {
    echo "<tr><td colspan=\"3\" align=\"center\">You have finished scratching!<br><a href=\"card.php?go=1\">Another?</a></td></tr>\n";
}


echo "</table>\n";
echo "<pre>\n";
print_r($_SESSION);
?>

Link to comment
https://forums.phpfreaks.com/topic/91756-session-problem/
Share on other sites

sorry for delay, you have a logic error while checking the y session variable:

 

there is no easy way to order an array by part of its value, so session['y'] is not reaching 6 before it gets to an unmarked variable.

 

like so:

 

loop1 = 1 (clicked_1) prize

loop2 = 2 (clicked_2) prize

loop3 = 2 (3) unmarked variable, will return with link as Y is still only "2"

loop4 = 3 (clicked_3) prize

loop5 = 4 (clicked_4) prize

loop6 = 4 (6) unmarked variable, will return with link as Y is still only "4"

loop7 = 5 (clicked_5) prize

loop8 = 6 (clicked_6) prize

loop9 = 6 (9) unmarked variable, will NOT return link as Y == "6".

 

do you understand?

 

you need to add another session variable to count the clicks as the user "clicks" and carry that number per page.

 

ex:

<?php
session_start();

$prizes = array('10000', '5000', '2500', '1000', 'blank', 'item', 'blank', '500',
    'blank', 'blank');

if (!@$_SESSION['clicked']) { // added @ symbol to suppress E_NOTICE (undefined index, variable does not exist)
    $_SESSION['clicked'] = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
    $_SESSION['prizes'] = array();

    foreach ($_SESSION['clicked'] as $clicked) {

        $_SESSION['prizes'][] = array_rand($prizes);
    }
}
$clickz = @$_GET['click'];

if ($clickz && $_SESSION['tclicks'] < 6) { // prevent user adding click request manually
    if (in_array($clickz, range(1, 9))) {
        $_SESSION['clicked'][$clickz - 1] = "clicked_" . $_SESSION['prizes'][$clickz - 1];
$_SESSION['tclicks']++; // tclicks added to bypass logic error
        header("Location: card.php");
    }
}

$go = @$_GET['go']; // added @ symbol to suppress E_NOTICE (undefined index, variable does not exist)
if ($go) {
    session_destroy();
    header("Location: card.php");
}


echo "<table border=\"1\" cellpadding=\"3\" cellpadding=\"3\" width=\"20%\">\n";
echo "<tr>\n";
$x = 1;
$_SESSION['y'] = 0;

foreach ($_SESSION['clicked'] as $click) {


    $ex = explode("_", $click);
    if (count($ex) > 1) {
        $echo = $prizes[$ex[1]];
    } else {
        if (@$_SESSION['tclicks'] >= 6) { // tclicks added to bypass logic error
            $echo = $_SESSION['prizes'][$click - 1];
        } else {
            $echo = "<a href=\"card.php?click=" . $click . "\">" . $_SESSION['prizes'][$click - 1] . "</a>";
        }
        
    }


    echo "<td align=\"center\" valign=\"middle\">" . $echo . "</td>\n";

    if ($x == 3) {
        echo "</tr><tr>\n";
        $x = 0;
    }

    $x++;
}
echo "</tr>\n";
if (@$_SESSION['tclicks'] >= 6) { // tclicks added to bypass logic error
    echo "<tr><td colspan=\"3\" align=\"center\">You have finished scratching!<br><a href=\"card.php?go=1\">Another?</a></td></tr>\n";
}


echo "</table>\n";
echo "<pre>\n";
print_r($_SESSION);
?>

 

hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/91756-session-problem/#findComment-470197
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.