-1

I create session start every link when its click.
but session start only token code
I want to have a token session when I click on every link
for example

<a href="member.php?token=4r5tfgbvf&id=1" target="_blank">Click me1</a><br>
<a href="member.php?token=fgvbhnj&id=2" target="_blank">Click me2</a><br>

When i clik "click me2" then session create

<?php
$_SESSION['token'] = "fgvbhnj";<br>
?>
but i not using javascript only php
if possible javascript used
how can this
code here

.member{
padding:5px 5px;
background:#e7f9eb;
overflow:auto;
}
.memo {
float:left;width:242px;
margin:5px;
padding:5px 5px;
background:#fff;
}
<div class="member">
<?php
for( $i1 = 0; $i1 <= 5; $i1++ ) {
for ($s = '', $i = 0, $z = strlen($a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=#$@!^&')-1; $i != 10; $x = rand(0,$z), $s .= $a{$x}, $i++);
?>
<a href="membershow.php?token=<?php echo $s; ?>&id=<?php echo $i1; ?>" target="_target">
<div class="memo">
<?php echo $i1; ?>
</div>
</a>
<?php
}
?>
</div>

1 Answers1

-1

You can store data in the cookie and retrieve the data. You can create cookie like the following -

function writeCookie(name,value,days) {
  var date, expires;
  if (days) {
    date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires=" + date.toGMTString();
        }else{
    expires = "";
  }
  document.cookie = name + "=" + value + expires + "; path=/";
}

Then in each page you need this session Id you can read the cookie, with a function like:

function readCookie(name) {
  var i, c, ca, nameEQ = name + "=";
  ca = document.cookie.split(';');
  for(i=0;i < ca.length;i++) {
    c = ca[i];
    while (c.charAt(0)==' ') {
        c = c.substring(1,c.length);
    }
    if (c.indexOf(nameEQ) == 0) {
        return c.substring(nameEQ.length,c.length);
    }
  }
  return '';
}

The read function work from any page or tab of the same domain that has written it, either if the cookie was created from the page in javascript or from the server. To store the id in the cookie -

var sId = 's234543245';
writeCookie('sessionId', sId, 3);

To read the cookie -

var sId = readCookie('sessionId')
santanu bera
  • 1,281
  • 10
  • 16
  • Although Session is uses cookie ( as a key to retrieve session data from server) IT doesn't work like browser cookie where data is exposed to user and stored at browser, so i dont see your answer is of any help, also it is better to write code instead of copy and past from https://stackoverflow.com/q/19068812/1723893 and https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username – NullPoiиteя Jun 30 '18 at 05:28
  • Though I used the example from w3schools, but I also wrote how to use this methods so that the he can find what he is looking for. All he needs a solution to his question. It doesn't matter where this solution come from. And also not every developer have all unique solution – santanu bera Jun 30 '18 at 06:09