在cookie中查找并​​保存变量

时间:2016-07-10 11:07:03

标签: javascript php

我正在尝试将变量保存在cookie中然后检索它,到目前为止我只能保存一个文本和页面URL,然后在任何我想要的地方检索它。

你可以帮我解决一下吗?例如在这段代码中我想在cookie中保存$ room和$ renthome值。

这是我目前的代码



private  class MyDragShadowBuilder extends View.DragShadowBuilder {

    // The drag shadow image, defined as a drawable thing
    private  Drawable shadow;
    private Point mScaleFactor;

    // Defines the constructor for myDragShadowBuilder
    public MyDragShadowBuilder(View v, int count) {

        // Stores the View parameter passed to myDragShadowBuilder.
        super(v);

        // Creates a draggable image that will fill the Canvas provided by the system.
       //            shadow = v
        shadow = new TextDrawable(getActivity(),"5000");

      //            shadow = new ColorDrawable(Color.LTGRAY);
        //ColorDrawable(Color.RED);

    }

    // Defines a callback that sends the drag shadow dimensions and touch point back to the
    // system.
    @Override
    public void onProvideShadowMetrics(Point size, Point touch) {
        // Defines local variables
        int width, height;

        // Sets the width of the shadow to half the width of the original View
        width = getView().getWidth() / 2;
        Log.d("TAG", "width=" + width);

        // Sets the height of the shadow to half the height of the original View
        height = getView().getHeight() / 2;

        Log.d("TAG", "height=" + height);


        // The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
        // Canvas that the system will provide. As a result, the drag shadow will fill the
        // Canvas.
        shadow.setBounds(0, 0, width, height);

        // Sets the size parameter's width and height values. These get back to the system
        // through the size parameter.
        size.set(width, height);
        // Sets size parameter to member that will be used for scaling shadow image.
        mScaleFactor = size;

        // Sets the touch point's position to be in the middle of the drag shadow
        touch.set(width / 2, height / 2);
    }

    // Defines a callback that draws the drag shadow in a Canvas that the system constructs
    // from the dimensions passed in onProvideShadowMetrics().
    @Override
    public void onDrawShadow(Canvas canvas) {

        // Draws the ColorDrawable in the Canvas passed in from the system.
        shadow.draw(canvas);
       // canvas.scale(mScaleFactor.x/(float)getView().getWidth(), mScaleFactor.y/(float)getView().getHeight());
      //  getView().draw(canvas);
    }
}




3 个答案:

答案 0 :(得分:0)

我使用jQuery Cookie

https://github.com/carhartl/jquery-cookie

用法:$.cookie('name', 'value');

检索:$.cookie('name'); // => "value"

要在Cookie中保存复杂数据,请执行以下操作:

1)将整个数据保存在一个对象中。像var cookieData = {};之类的东西,然后您可以添加cookieData.date = yourDate; cookieData.myUrl=yourUrl;

等属性

2)使用JSON.stringify

将对象转移到sting

3)将字符串保存在cookie中。

现在,为了再次使用它 -

1)检索cookie(参见上面的代码)

2)使用JSON.parse

将字符串转回对象

3)从对象中获取所需的数据

答案 1 :(得分:0)

在jQuery中,您应该使用jquery.cookie插件。

这会创建一个cookie

$.cookie(name, value [, options]);

然后检索它的值

$.cookie(name);

答案 2 :(得分:0)

您可以在PHP <script>块中生成将保存变量的块。示例显示按名称room保存名为$room的Cookie。

<script>
     createCookie("room", "<?= $row['room'] ?>", 2);
</script>

另一种(我认为更好)方法是通过PHP设置cookie。

<?php setcookie("room", $row['room'], 2 * 24 * 60 * 60 * 1000); ?>

不要忘记,如果你想通过PHP设置cookie,你必须在发送任何输出之前做到这一点。在您的情况下,您必须准备输出,设置cookie然后发送输出。