Jump to content

mobilestimulus

Members
  • Posts

    12
  • Joined

  • Last visited

About mobilestimulus

  • Birthday 03/24/1978

Profile Information

  • Gender
    Male
  • Location
    Las Vegas

mobilestimulus's Achievements

Member

Member (2/5)

0

Reputation

  1. Okay, working as expected. Two things: I should be using: htmlspecialchars($_GET["link_order"]); to prevent injection. Correct? Is there anything else I should do to protect this code?
  2. Wait. I think it is working. I cleared the cookie. Let me test. Again, thank you so much....
  3. Ha! Yes. An extra space....Fixed. Still not as expected however. Do I need to do more yet? Code as of now: <?php // cookie information $cookie_name = "link_order_test"; // cookie expires 2 minutes from page load $cookie_expires_time = 120; // links $links = [ 1 => [ "url" => "http://www.google.com/", "text" => "Google" ], 2 => [ "url" => "https://forums.phpfreaks.com/topic/307814-how-to-rotate-links-using-php-and-cookie/?tab=comments#comment-1561732", "text" => "phpfreaks.com" ], 3 => [ "url" => "https://www.w3schools.com/", "text" => "w3schools" ], 4 => [ "url" => "http://php.net/manual/en/language.types.array.php", "text" => "php.net" ] ]; // list of links to use from the cookie if (isset($_COOKIE[$cookie_name])) { $number_set = $_COOKIE[$cookie_name]; } else { $number_set = '1,2,3,4'; } $pieces = explode(",", $number_set); $pieces = array_combine($pieces, $pieces); // update if there is an update to make if (isset($_GET["link_order"])) { $link_order = $_GET["link_order"]; if (isset($pieces[$link_order])) { unset($pieces[$link_order]); $pieces[$link_order] = $link_order; } $number_set = implode(",", $pieces); // array back into string setcookie($cookie_name, $number_set, time() + $cookie_expires_time); } // loop to show the links echo '<ul>'; foreach ($pieces as $number) { $value = $links[$number]; echo '<li><a href="/test/?link_order=' . $number . '" onClick="window.open(\'' . $value["url"] . '\');">' . $value["text"] . '</a></li>'; } echo '</ul>'; ?>
  4. It doesn't work now, but did earlier. something about this line I guess: $number_ set = implode(",", $pieces); // array back into string
  5. @requinix Alright. So, I don't understand what is happening yet, but I will put some more time into learning about it. In the meantime, are you saying that I should now be here?: <?php // cookie information $cookie_name = "link_order_test"; // cookie expires 2 minutes from page load $cookie_expires_time = 120; // links $links = [ 1 => [ "url" => "http://www.google.com/", "text" => "Google" ], 2 => [ "url" => "https://forums.phpfreaks.com/topic/307814-how-to-rotate-links-using-php-and-cookie/?tab=comments#comment-1561732", "text" => "phpfreaks.com" ], 3 => [ "url" => "https://www.w3schools.com/", "text" => "w3schools" ], 4 => [ "url" => "http://php.net/manual/en/language.types.array.php", "text" => "php.net" ] ]; // list of links to use from the cookie if (isset($_COOKIE[$cookie_name])) { $number_set = $_COOKIE[$cookie_name]; } else { $number_set = '1,2,3,4'; } $pieces = explode(",", $number_set); $pieces = array_combine($pieces, $pieces); // update if there is an update to make if (isset($_GET["link_order"])) { $link_order = $_GET["link_order"]; if (isset($pieces[$link_order])) { unset($pieces[$link_order]); $pieces[$link_order] = $link_order; } $number_ set = implode(",", $pieces); // array back into string setcookie($cookie_name, $number_set, time() + $cookie_expires_time); } // loop to show the links echo '<ul>'; foreach ($pieces as $number) { $value = $links[$number]; echo '<li><a href="/test/?link_order=' . $number . '" onClick="window.open(\'' . $value["url"] . '\');">' . $value["text"] . '</a></li>'; } echo '</ul>'; ?>
  6. So I think I have all of the parts here, just not sure how to implement them. Again, any more help you're willing to give is appreciated. Here is what I have now: <?php $link_order = htmlspecialchars($_GET["link_order"]); $cookie_name = "link_order_test"; $cookie_expires_time = '1440'; if(isset($_COOKIE[$cookie_name])) { $number_set = $_COOKIE[$cookie_name]; }else{ $number_set = '1,2,3,4'; echo 'Cookie Not Found <br />'; } $pieces = explode(",", $number_set); $links = [ 1 => ["url" => "http://www.google.com/", "text" => "Google", "number" => "1"], 2 => ["url" => "https://forums.phpfreaks.com/topic/307814-how-to-rotate-links-using-php-and-cookie/?tab=comments#comment-1561732", "text" => "phpfreaks.com", "number" => "2"], 3 => ["url" => "https://www.w3schools.com/", "text" => "w3schools", "number" => "3"], 4 => ["url" => "http://php.net/manual/en/language.types.array.php", "text" => "php.net", "number" => "4"] ]; foreach ($pieces as $value1) { echo "$value1<br>"; } $id = $link_order; if (isset($links[$id])) { $link = $links[$id]; // item in $links array unset($links[$id]); // remove $links[$id] = $link; } echo '<ul>'; foreach ( $links as $key => $value ) { echo '<li><a href="/test/?link_order='.$value[number].'" onClick="window.open(\''.$value[url].'\'); setCookie(\''.$cookie_name.'\', \''.$value[number].'\', '.$cookie_expires_time.');">'.$value[text].'</a></li>'; } echo '</ul>'; if(isset($_COOKIE[$cookie_name])) { echo '<a href="/test/" onClick="setCookie(\''.$cookie_name.'\', \'0\', -'.$cookie_expires_time.');">Reset List</a>'; } echo '<script> function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*60*1000)); var expires = "expires="+d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; } </script>'; ?>
  7. @requinix I'm having trouble with this part. Can you give me a "push" in the right direction? Here is what I have now: <?php $link_order = htmlspecialchars($_GET["link_order"]); $cookie_name = "link_order_test"; $cookie_expires_time = '1440'; $links = [ 1 => ["url" => "http://www.google.com/", "text" => "Google"], 2 => ["url" => "https://forums.phpfreaks.com/topic/307814-how-to-rotate-links-using-php-and-cookie/?tab=comments#comment-1561732", "text" => "phpfreaks.com"], 3 => ["url" => "https://www.w3schools.com/", "text" => "w3schools"], 4 => ["url" => "http://php.net/manual/en/language.types.array.php", "text" => "php.net"] ]; $id = $link_order; if (isset($links[$id])) { $link = $links[$id]; // item in $links array unset($links[$id]); // remove $links[$id] = $link; } $pieces = explode(" ", $links[$key]); print_r($pieces); echo '<ul>'; foreach ( $links as $key => $value ) { echo '<li><a href="/test/?link_order='.$key.'" onClick="window.open(\''.$value[url].'\'); setCookie(\''.$cookie_name.'\', \''.$key.'\', '.$cookie_expires_time.');">'.$value[text].'</a></li>'; } echo '</ul>'; ?>
  8. @requinix Okay, again, I'm getting there, slowly, but making progress. Your help is appreciated. However, with each link visit, all that happens is the visited link goes to the bottom of the list. Is that because the cookie doesn't have information about the current order of the list? For instance, on the first visit, I use Link 1, the new order is 2 3 4 1. Now, if i visit link 3 next, the order should now be 2 4 1 3, but instead the order is now 1 2 4 3. Am I still messing up, or do I need to add something else? My code so far: $cookie_name = "link_order_test"; $cookie_expires_time = '1440'; $links = [ 1 => ["url" => "http://www.google.com/", "text" => "Google"], 2 => ["url" => "https://forums.phpfreaks.com/topic/307814-how-to-rotate-links-using-php-and-cookie/?tab=comments#comment-1561732", "text" => "phpfreaks.com"], 3 => ["url" => "https://www.w3schools.com/", "text" => "w3schools"], 4 => ["url" => "http://php.net/manual/en/language.types.array.php", "text" => "php.net"] ]; if(isset($_COOKIE[$cookie_name])) { $id = $_COOKIE[$cookie_name]; if (isset($links[$id])) { $link = $links[$id]; // item in $links array unset($links[$id]); // remove $links[$id] = $link; } } else { echo 'Cookie Not Found'; } echo '<ul>'; foreach ( $links as $key => $value ) { echo '<li><a href="/test/" onClick="window.open(\''.$value[url].'\'); setCookie(\''.$cookie_name.'\', \''.$key.'\', '.$cookie_expires_time.');">'.$value[text].'</a></li>'; } echo '</ul>'; echo '<a href="/test/" onClick="setCookie(\''.$cookie_name.'\', \'0\', -'.$cookie_expires_time.');">Reset List</a>'; echo '<script> function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*60*1000)); var expires = "expires="+d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires; } </script>';
  9. Okay. Thank you so much! Way more helpful than other places for sure! I think I am almost there... What goes here?: $link = // item in $links array
  10. I appreciate you, and I'm learning. I'm sure I'm getting it wrong, as this is not as expected: $links = [ 1 => ["url" => "http://www.example.com/page-1", "text" => "Link One"], 2 => ["url" => "http://www.example.com/page-1", "text" => "Link Two"], 3 => ["url" => "http://www.example.com/page-1", "text" => "Link Three"], 4 => ["url" => "http://www.example.com/page-1", "text" => "Link Four"] ]; foreach ( $links as $key => $value ) { echo "$key=$value<br />"; }
  11. Hello! I'm new here, and this is my first post. Let's say I have a list of four links I want to display on a page using php, call them 1 2 3 and 4. Now, when link 2 is visited, or any of the links, the destination url opens in a new window, a cookie is set onclick and the page reloads, and the visited link goes to the bottom of my list, and the new the new order is: 1 3 4 2. php function to read cookie and calculate the new order here echo '<ul> <li> <a href="https://current-page" onClick="window.open(\'http://page-1\'); setCookie(\''.$cookie_name.'\', \'1\', '.$cookie_expires_time.');">Link One</a> </li> <li> <a href="https://current-page" onClick="window.open(\'http://page-2\'); setCookie(\''.$cookie_name.'\', \'2\', '.$cookie_expires_time.');">Link Two</a> </li> <li> <a href="https://current-page" onClick="window.open(\'http://page-3\'); setCookie(\''.$cookie_name.'\', \'3\', '.$cookie_expires_time.');">Link Three</a> </li> <li> <a href="https://current-page" onClick="window.open(\'http://page-4\'); setCookie(\''.$cookie_name.'\', \'4\', '.$cookie_expires_time.');">Link Four</a> </li> </ul>'; echo 'some javascript function to enable set cookie on click'; Any help is much appreciated. Thanks.
×
×
  • 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.