Cookie设置后返回null?

时间:2017-09-24 01:06:56

标签: php laravel

我试图永远设置一个cookie,但是当我尝试获取它时它返回null? (出口线)。

public class Task3Ball implements Runnable {

    private double radius = 20;
    private double x = radius*2, y = radius*2;
    private double dx = 1, dy = 1;
    private double speed = 3.0;
    public Circle circle = new Circle(radius,Color.GREEN);

    /** Returns the current speed of the ball.
     * @return (String) the speed as a string, formatted to two decimal places
     */
    public String getSpeed() {
        return String.format("%.2f", speed);
    }

    /** Increases the speed of the ball by 0.1 to a maximum of 5.
     */
    public void increaseSpeed() {
        speed += 0.1;
        if (speed > 5)
            speed = 5;
    }

    /** Decreases the speed of the ball by 0.1 to a minimum of 1.
     */
    public void decreaseSpeed() {
        speed -= 0.1;
        if (speed < 1)
            speed = 1;
    }

    /** Moves the ball based on its current speed and direction.
     * Reverses the direction of the ball when it collides with the edge of the window.
     * Updates the circle object to reflect its current position.
     */
    protected void moveBall() {

        if (x-radius <= 0 || x+radius >= 400)
            dx *= -1;

        if (y-radius <= 0 || y+radius >= 300)
            dy *= -1;

        x += dx*speed;
        y += dy*speed;
        circle.setCenterX(x);
        circle.setCenterY(y);
    }

    /** Uses a thread to move the ball every 20 milliseconds.
     */
    public void run() {

        while(true) {
            try {
                moveBall();
                Thread.sleep(20);
            }
            catch(InterruptedException e) {
                System.out.println("interrupt");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

按照设计,只有在下一页加载后才会出现cookie。 Laravel使用setcookie PHP函数,并在the documentation中包含它:

  

设置好cookie后,可以在下一页加载$ _COOKIE数组时访问它们。

我不确定您使用的是什么版本的Laravel,但在v5.0 +中有Cookie::queued方法。