用于存储、检索和附加到url的javascript cookie

用于存储、检索和附加到url的javascript cookie,javascript,html,cookies,Javascript,Html,Cookies,我有两个html页面 第1页有这行html代码 第2页有 如何使用cookie存储和获取用户id,并将其附加到一个有效期为一个月的href标签 希望你们这些极客能提供一个有用的例子。试试这个,它会对你们有所帮助 页面1.html <html> <head> <script type="text/javascript"> <!-- function uid() { if( document.getElementById("userid").

我有两个html页面

第1页有这行html代码

  • 第2页有





    如何使用cookie存储和获取用户id,并将其附加到一个有效期为一个月的href标签


    希望你们这些极客能提供一个有用的例子。

    试试这个,它会对你们有所帮助

    页面1.html

    <html>
    <head>
    <script type="text/javascript">
    <!--
    function uid()
    {
       if( document.getElementById("userid").value == "" ){
          alert("Enter some value!");
          return;
     }
    
      var now = new Date();
      now.setMonth( now.getMonth() + 1 ); 
      document.cookie = "expires=" + now.toUTCString() + ";"
      cookievalue= escape(document.getElementById("userid").value) + ";";
      document.cookie="id=" + cookievalue;
      document.cookie = "expires=" + now.toUTCString() + ";"
      document.cookie="id=" + cookievalue;
    
    
      alert("Setting Cookies : " + "id=" + cookievalue );
    }
    //-->
    </script>
    </head>
    
    <body>
    
        <form action="Page2.html">
        <input type="text" name="userid" id="userid" type="text" />
        <input type="submit" onclick="uid()" value="Store Cookie and proceed to next pg">
        </form>
    </body>
    </html>
    
    
    
    页面2.html

    <html>
    <head>
    <script type="text/javascript">
    <!--
    var value;
    var id;
    function ReadCookie()
    {
       var allcookies = document.cookie;
    
       // Get all the cookies pairs in an array
       cookiearray  = allcookies.split(';');
    
       // Now take key value pair out of this array
       for(var i=0; i<cookiearray.length; i++){
          id = cookiearray[i].split('=')[0];
          value = cookiearray[i].split('=')[1];
          if(id.trim()==="id"){
          document.getElementById('wel').innerHTML="welcome "+value;
          alert("Key is : " + id + " and Value is : " + value);
          break;
          }  
     }
    }
    
    function app(u)
    {
      alert(u+value);
    location.href = u+value;
    //window.location.replace(u+value);
    return false;    
    }
    
    //-->
    </script>
    </head>
    
    <body onload="ReadCookie()">
    
    <p id="wel"></P>
    <a href="Profile.html?id=" onclick="app(this.href);return false;"> Profile</a><br/>
     <a href="art.html?id=" onclick="app(this.href);return false;">art</a><br/>
     <a href="account.html?id=" onclick="app(this.href);return false;">account</a><br/>
     <a href="link.html?id=" onclick="app(this.href);return false;">link</a><br/>
    </body>
    
    </html>
    
    
    






    我太忙了,但我想这可能会有帮助

    看完这篇文章

    我发现,; HTML本地存储为在客户端上存储数据提供了两个对象:

    window.localStorage-存储没有过期日期的数据 window.sessionStorage-存储一个会话的数据(关闭选项卡时数据丢失)

    所以我想到了使用Php来完成解决方案的想法

    <scripts>
    function SaveId(id){
    $.ajax({
    url: "side.php?newId="+id,
    success: function (data) {
    //do some thing you could append the data to an element attribute
    }//sucess
    });
    
    }//Save Id function close here
    
    $("body").find("li").click(function (){
    var newId=$(this).attr("data-user-id");
    SaveId(newId);//fire the SaveId()
    });
    </scripts>
    
    
    函数SaveId(id){
    $.ajax({
    url:“side.php?newId=“+id,
    成功:功能(数据){
    //做一些可以将数据附加到元素属性的事情
    }//成功
    });
    }//保存Id函数在此处关闭
    $(“body”)。查找(“li”)。单击(函数(){
    var newId=$(this.attr(“数据用户id”);
    SaveId(newId);//激发SaveId()
    });
    
    ==服务器端==Side.php

    <?php
     if(isset($_REQUEST['newId'])){
    $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;//Locahost helper
    
    $newQ=$_REQUEST['newId'];
    if (isset($_COOKIE['userId'])){
    echo $_COOKIE['userId'];//return store user Id we already have one 
    }
    else
    {
    setcookie('userId', $newQ, time()+60*60*24*30, '/', $domain, false);//1 Month you said
    echo $_REQUEST['newId'];//As it is not available in storage until page reload
    }
    }//isest ends here
    ?>
    
    
    
    在第一页上

    <?php 
    //after login get user id from somewhere
    $userId=$_REQUEST['userId'];
    ?>
    
    <li data-user-id=<?php echo $userId; ?> id= > I am first List </li>
    
    
    
    请原谅。。。我正在寻找一个学校项目的本地主机、客户端java脚本解决方案。您如何编写此文件,以便只保存用户ID,然后将其附加到url链接而不过期?如果您希望保留例如10年时间()+60*60*24*365*10。有关cookie规范,请阅读更多有关如何应用cookie打印功能的信息。。。而不是警惕?。。。。欢迎打印(idValue)@Rajesh您可以使用write方法执行此操作,例如:document.write(“欢迎”+value);请注意docoument.write()是javascript方法,所以请将其放在tag@RajeshPardhan我已经编辑了我的答案,您可以查看欢迎信息。如果代码有任何其他问题,请告诉我
    <?php $saraId=$_COOKIE['userId'];?>
    
    <a href="whereToLink/?id=<?php echo $saraId ?>" >LinkOn</a>