PHP逻辑运算符组合

时间:2016-11-23 11:21:21

标签: php

我使用一些PHP来重定向网站访问者,如果他们没有使用CMS的成员插件登录。我有这个代码工作正常:

if (!perch_member_logged_in() && ($_SERVER["REQUEST_URI"] !== '/')){
  PerchSystem::redirect('/');
}

我现在想要添加一个查询字符串,以查看是否有人被重定向,并显示相应的消息。如果有人直接进入主页,我不想显示这个。我试过这个:

if (!perch_member_logged_in() && (($_SERVER["REQUEST_URI"] !== '/') || ($_SERVER["REQUEST_URI"] !== '/?redirect=true'))){
  PerchSystem::redirect('/?redirect=true');
}

但是我的重定向太多了。任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

我想你只需要这个:

if (!perch_member_logged_in() && $_SERVER["REQUEST_URI"] !== '/?redirect=true') {
  PerchSystem::redirect('/?redirect=true');
}

如果用户未登录并进入您的页面,他可以登陆以下三个页面之一:

  • 主页 - 什么都不做
  • 设置了redirect=true的主页 - 也无所事事
  • 任何其他网页 - 使用redirect=true
  • 重定向到主页

答案 1 :(得分:0)

首先,我想指出PHP手册中的Operator Precedence page。它解释了运算符的评估顺序,这可以帮助你摆脱代码中的一些不必要的括号。
我怀疑这也可以解决你的问题,这取决于你的意思“太多了重定向“。

尽管如此,我怀疑您也不需要在root访问时检查重定向。很难说肯定,因为你没有正确解释你的问题是什么,以及你期望发生什么 换句话说,我(也)认为你的代码应该是这样的:

using UnityEngine;
using System.Collections;

public class ControlShip : MonoBehaviour {

    public int rotationSpeed = 75;
    public int movementspeed = 10;
    private int thrust = 5;

    bool isPKeyDown = false;
    float acceleration = .0f;

    Vector3 previousPosition = Vector3.zero;

    Rigidbody _rigidbody;

    // Use this for initialization
    void Start () {

        _rigidbody = GetComponent<Rigidbody>();
        Debug.Log("Acc Speed: " + thrust);
    }

    // Update is called once per frame
    void Update () {

        var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
        transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
        transform.position += transform.forward * Time.deltaTime * movementspeed;

        if (Input.GetKey(KeyCode.Z))
            transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);

        if (Input.GetKey("p"))
        {
            isPKeyDown = Input.GetKey("p");
            float distance = Vector3.Distance(previousPosition, transform.position);
            float acceleration = distance / Mathf.Pow(Time.deltaTime, 2);

            previousPosition = transform.position;
            _rigidbody.AddRelativeForce(0f, 0f, acceleration, ForceMode.Acceleration);
        }
    }

    void OnGUI()
    {
        if (isPKeyDown)
        {
            GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
        }
    }
}

正如那段代码所说:如果用户未登录,并且设置了重定向标志,则重定向用户。

您应该做的另一件事是检查if (!perch_member_logged_in() && $_SERVER["REQUEST_URI"] !== '/?redirect=true'){ PerchSystem::redirect('/?redirect=true'); } 方法中的代码,并确保在发送PerchSystem::redirect()标头后使用die()。否则,您的代码将继续执行,并导致不必要的行为和/或安全问题。

答案 2 :(得分:0)

原来我需要这个:

if (!perch_member_logged_in() && ($_SERVER["REQUEST_URI"] !== '/') && ($_SERVER["REQUEST_URI"] !== '/?redirect=true')){
  PerchSystem::redirect('/?redirect=true');
}

感谢所有花时间回应的人,我很感激

相关问题