0

if I have two links and I want to show both of them continuously back to back, then what should i use in php?

For Example:

First show link A then show link B then again show link A and again show link B. Show both of these links after each other.

When I click on page it show link A when I refresh page it shows link b and vice versa.

So my questions is, Is there any any php function or logic for this if yes then what is it?

King
  • 23
  • 8

3 Answers3

0

If you want to change the page to B after clicking the A link, you can just store a parameter in the link (for example, <a href="website.com?nextLink=B">A</a>), then catch it server side and serve the page with the B link (you could use many tricks to have the exact same URL, a POST parameter instead of a GET one for example).

If you want to change the state on hitting refresh, you can use cookies to store the current state, then use this state server side to switch the page (adviced here: Detect Browser Refresh in Javascript).

Community
  • 1
  • 1
Robin
  • 9,415
  • 3
  • 34
  • 45
0
<?php
   if ( isset($_GET['page']))
      $page = ($_GET['page'] == 'a') ? 'b' : 'a'; 
   else
      $page = 'b';         
?>


<a href="index.php?page=<?php echo $page; ?>">Cycle</a>

One question: wherefore it?

voodoo417
  • 11,861
  • 3
  • 36
  • 40
0
<?php
if(!isset($_COOKIE["lastLink"])){
    setcookie("lastLink","b"); // you can set an expire time.
}

if($_COOKIE["lastLink"] == "a"){
    $link = "www.b.com";
    setcookie("lastLink","b");
}elseif($_COOKIE["lastLink"] == "b"){
    $link = "www.a.com";
    setcookie("lastLink","a");
}

echo $link;
?>

About expire time and more: http://uk.php.net/setcookie

Melih Yıldız'
  • 415
  • 5
  • 17
  • What if don't set expire time for cookie, What will be the default expiration time? – King Jan 24 '14 at 09:37
  • 1
    Your this code show only www.a.com every time I refresh page. – King Jan 24 '14 at 09:57
  • My bad, you cant change a cookie like this. you must change `$_COOKIE["lastLink"] = "a"` and `$_COOKIE["lastLink"] = "b"` to `setcookie("lastLink","a");` and `setcookie("lastLink","b");` – Melih Yıldız' Jan 24 '14 at 20:39
  • What if a user come and click the link and it will set the cookie, then he leave and another user clicks and so on. So cookie is set on user computer. like that only my link a is clicking by user but link b is didnt clicked yet. Then What should I do in that case? @Melih Yıldız – King Jan 24 '14 at 21:17
  • If you wanna change the link on the server, you can use a file record or a database record instead of cookie. If my server would use SSD, I'd prefer file record to not harm database server. But it's on your choice. Just change `$_COOKIE["lastLink"]` parts with the variable read from record and `setcookie()` parts with the function changes the record. – Melih Yıldız' Jan 29 '14 at 15:12
  • Thanks For Advice. Let me code this, if I stuck in any code I will contact with you. – King Jan 31 '14 at 16:21