Page 1 of 1

£20 reward for first solution to this:

Posted: Mon May 16, 2011 3:39 pm
by bobthebuilder
OK, follwing on from this post:

viewtopic.php?f=1&t=129755

I desperately need a solution. I am offering £20 (or equivalent currency) payable through Paypal to solve my problem. I have produced a really cut down test-case here:

Code: Select all

<?php
if(!isset($_SESSION)) {
    session_start();
}
if(!isset($_SESSION['hits'])) {
    $_SESSION['hits'] = 0;
}
if( isset($_GET['image_shift'])) {   
    if( $_GET['image_shift'] == "right" ) {
        $_SESSION['hits'] = $_SESSION['hits'] + 1;
    }
    if( $_GET['image_shift'] == "left" ) {
        $_SESSION['hits'] = $_SESSION['hits'] + 1;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rotate images</title>
</head>

<body text="#000000" style="background-color:#f1f0f1; text-align:center; height:1400px;">
<div style="background-color:#ffffff;text-align:left;margin-left:auto;margin-right:auto;position:relative;width:800px;height:1400px;">


<?php

echo "<div style=\"position:absolute; left:410px; top:420px; width:100px; height:90px;\">";
echo "<a href=\"wrong.php?image_shift=right\">\n";
echo "Click right";
echo "</a>\n";
echo "</div>\n";


echo "<div style=\"position:absolute; left:100px; top:420px; width:100px; height:90px;\">";
echo "<a href=\"wrong.php?image_shift=left#bob\">\n";
echo "Click left";
echo "</a>\n";
echo "</div>\n";
    
    
    
    
echo "<div style=\"position:absolute; top:200px; left:250px;\">\n"; 
echo $_SESSION['hits'] . " hits on my counter<br />";
echo "</div>\n";  
    
    
echo "<div style=\"position:absolute; left:90px; top:600px; width:260px; height:90px;\">";
echo "<a name=\"bob\">text that the left hit jumps to</a>\n";
echo "</div>";

?>   
</div>
</body>
</html>
If you run this code you will see it goes wrong when you "Click left" when I am trying to run the code that links to a spot in the file. When you "Click right" the code works fine as there is no attempted use of an anchor. So either my understanding of php/html is wrong, or I have just made a basic mistake.

To claim the £20, I need
1) To know where I am going wrong
2) To be given the correct solution (i.e.code)

Please note, "Use javascript" is not a satisfactory reponse to get the Wonga. I can deal with php, html and CSS, but a JS solution is no good to me unless you provide the exact code that will work when I put it in my real code.

Any questions, please PM me. I am UK based, so there me a time delay.

If this is against forum rules I appologise in advance.

Re: £20 reward for first solution to this:

Posted: Mon May 16, 2011 7:01 pm
by mecha_godzilla
Hi,

Where you have this code:

Code: Select all

echo "<div style=\"position:absolute; left:100px; top:420px; width:100px; height:90px;\">";
echo "<a href=\"wrong.php?image_shift=left#bob\">\n";
echo "Click left";
echo "</a>\n";
echo "</div>\n";
is it supposed to be:

Code: Select all

echo "<div style=\"position:absolute; left:100px; top:420px; width:100px; height:90px;\">";
echo "<a href=\"wrong.php?image_shift=left\">\n";
echo "Click left";
echo "</a>\n";
echo "</div>\n";
I figure that's probably not enough to claim my 20 GBP :) Also, I'm not really sure what this script is supposed to be doing? Are you supposed to be adding the value in the 'hits' session if you click right and subtracting it if you go left? At the moment, both clicks do the same thing.

Mecha_Godzilla

Re: £20 reward for first solution to this:

Posted: Mon May 16, 2011 7:37 pm
by flying_circus
You need to add a nonce. I realize it's not the most elegant solution, it's a total hack, but it works.

Consider the following:

Code: Select all

<?php
  echo "<div style=\"position:absolute; left:100px; top:420px; width:100px; height:90px;\">";
  echo "<a href=\"wrong.php?image_shift=left&nonce=" . uniqid() . "#bob\">\n";
  echo "Click left";
  echo "</a>\n";
  echo "</div>\n";
?>

Re: £20 reward for first solution to this:

Posted: Mon May 16, 2011 9:07 pm
by flying_circus
mecha_godzilla wrote:I figure that's probably not enough to claim my 20 GBP :) Also, I'm not really sure what this script is supposed to be doing? Are you supposed to be adding the value in the 'hits' session if you click right and subtracting it if you go left? At the moment, both clicks do the same thing.

Mecha_Godzilla
It took me a minute to figure out what exactly the problem was as well. The problem is that if you click a link with an anchor in it (#bob), from a URL that already has the anchor in it, the page just jumps to the anchor, is not reloaded, and does not rack up an extra "page hit".

Navigation path on a page with only 2 URL's (left and right):
initial load of wrong.php (+1 hit)
click left with anchor (+1 hit)
click right (+1 hit)
click left with anchor (+1 hit)
click left with anchor (+0 hit)
click left with anchor (+0 hit)

Re: £20 reward for first solution to this:

Posted: Tue May 17, 2011 12:48 am
by bobthebuilder
I think flying_circus may have the solution - thanks! I shall try it out tonight after work and then assuming your solution is correct, I shall be in touch!

Re: £20 reward for first solution to this:

Posted: Tue May 17, 2011 1:01 pm
by McInfo
Pagination that identifies pages by "left" or "right" is stable only within a session. The URLs cannot be saved as bookmarks, nor shared through email, with predictable results.

Re: £20 reward for first solution to this:

Posted: Tue May 17, 2011 1:07 pm
by bobthebuilder
The "left" or "right" words are only being used as unique parameters to the $_GET process, so hopefully there's no problem there, unless I have completely missed something?

Re: £20 reward for first solution to this:

Posted: Tue May 17, 2011 2:06 pm
by bobthebuilder
The good news for flying_circus is that with my test code, his solution solves the problem so he gets the cash.

The bad news for me is that when I use this techniques with my real - much more complicated code - I get the same problem :banghead:

Thankfully I have stumbled across a hack that works in both instances.

The link in the code is now

Code: Select all

echo "<a href=\"wrong.php?image_shift=right\">\n";      
i.e. there is no anchor. The $_GET processes the image_shift?right construct and then does 'the work' in this case.

But I then use

Code: Select all

if( isset($_GET['image_shift'])) {   
    if( $_GET['image_shift'] == "right" ) {
        $_SESSION['hits'] = $_SESSION['hits'] + 1;
        header('Location: http://localhost/web/webplus_php/wrong.php#bob');
        exit;
    }
    if( $_GET['image_shift'] == "left" ) {
        $_SESSION['hits'] = $_SESSION['hits'] + 1;
        header('Location: http://localhost/web/webplus_php/wrong.php#bob');
        exit;
    }
}
i.e. I load the page again with no php parameters, just the anchor. This seems to work fine. Very inelegant, but I can't spend any more time on this and at least its a solution of sorts.

Re: £20 reward for first solution to this:

Posted: Tue May 17, 2011 4:42 pm
by mecha_godzilla
bobthebuilder wrote:The good news for flying_circus is that with my test code, his solution solves the problem so he gets the cash.
Damn! That was my best chance of finally getting some food to eat this week :) If you don't hear from me, you'll know why...

M_G