在 PHP 中更好地从 GET 捕获会话会员 ID

发布于 2024-08-17 18:33:00 字数 1050 浏览 10 评论 0原文

有人可以帮我清理这个并使其更符合逻辑吗?我现在很困惑,似乎写不出一行好的代码:)

我正在尝试从像 ?aid=3056677 这样的 URL 中捕获附属 ID。这个想法是如果在 GET 中设置了 aff id 则优先,会话和最后的 cookie 优先。另外,我们不想设置一个不存在的 aff id。

您知道更可靠的方法吗?

session_start(); // start session

// affiliate id
$g_aid = (isset($_GET['aid']) && $_GET['aid'] != '') ? trim($_GET['aid']) : false;
$s_aid = (isset($_SESSION['aid']) && $_SESSION['aid'] != '') ? trim($_SESSION['aid']) : false;
$c_aid = (isset($_COOKIE['aid']) && $_COOKIE['aid'] != '') ? trim($_COOKIE['aid']) : false;

if($g_aid !== false) // use get if set
  $aid = $g_aid;
elseif($s_aid !== false) // next use session if get not set
  $aid = $s_aid;
elseif($c_aid !== false) // cookie
  $aid = $c_aid;
else
  $aid = ''; // leave it empty

// if $aid is set is it in the $affiliates array?
//If not use the first key from that array
$aid = (isset($affiliates[$aid])) ? $aid : key($affiliates);

// save it and set it
// (maybe shouldn't be done if already stored?
setcookie('aid', $aid);
$_SESSION['aid'] = $aid;

Can someone help me clean this up and make it more logical? I'm fried right now and can't seem to get a good line of code written :)

I'm trying to capture affiliate id from urls like ?aid=3056677. The idea is IF aff id is set in GET that takes precedence, the session and finally cookie with least. Also, we don't want to set an aff id that does not exist.

Do you know of a more tried and true method of doing this?

session_start(); // start session

// affiliate id
$g_aid = (isset($_GET['aid']) && $_GET['aid'] != '') ? trim($_GET['aid']) : false;
$s_aid = (isset($_SESSION['aid']) && $_SESSION['aid'] != '') ? trim($_SESSION['aid']) : false;
$c_aid = (isset($_COOKIE['aid']) && $_COOKIE['aid'] != '') ? trim($_COOKIE['aid']) : false;

if($g_aid !== false) // use get if set
  $aid = $g_aid;
elseif($s_aid !== false) // next use session if get not set
  $aid = $s_aid;
elseif($c_aid !== false) // cookie
  $aid = $c_aid;
else
  $aid = ''; // leave it empty

// if $aid is set is it in the $affiliates array?
//If not use the first key from that array
$aid = (isset($affiliates[$aid])) ? $aid : key($affiliates);

// save it and set it
// (maybe shouldn't be done if already stored?
setcookie('aid', $aid);
$_SESSION['aid'] = $aid;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢蛋碎的人ぎ生 2024-08-24 18:33:00
session_start();

// checks if a field is valid
function isValid($aid) {
    return (!empty($aid) && trim($aid) != '');
}

// set the affiliate ID
$aid = isValid($_GET['aid'])     ? $_GET['aid'] :
       isValid($_SESSION['aid']) ? $_SESSION['aid'] : 
       isValid($_COOKIE['aid'])  ? $_COOKIE['aid'] :
       '';

// use first key from array if aid not set
if (!isset($affiliates[$aid])) $aid = key($a);

// save and set 
setcookie('aid', $aid);
$_SESSION['aid'] = $aid;
session_start();

// checks if a field is valid
function isValid($aid) {
    return (!empty($aid) && trim($aid) != '');
}

// set the affiliate ID
$aid = isValid($_GET['aid'])     ? $_GET['aid'] :
       isValid($_SESSION['aid']) ? $_SESSION['aid'] : 
       isValid($_COOKIE['aid'])  ? $_COOKIE['aid'] :
       '';

// use first key from array if aid not set
if (!isset($affiliates[$aid])) $aid = key($a);

// save and set 
setcookie('aid', $aid);
$_SESSION['aid'] = $aid;
画骨成沙 2024-08-24 18:33:00
  1. 如果您从 $_GET 数组中获得了有效的会员 ID,为什么要测试会话和 cookie? ==> 使其渐进式,以便仅在未找到 GET 时检查会话,仅在未找到会话时检查 cookie。

  2. 不要重复验证affiliateID。 ==> 编写一个验证函数并重用它,稍后您可能需要添加更多规则。

  3. 使用大括号使您的代码更具可读性

  4. $aid 或 $aff 是不好的变量名称,而 $affiliateID 是一个好的变量名称!编写简短的变量名称不会带来任何好处,但编写不言自明的代码会带来很多好处。

示例,不说话

if (validate($aff)) 

示例,与你交谈

if (isValid($affiliationID))

所以我建议更改核心组件:

if (isValid($_GET['aid']))
{
    $affiliationID = trim($_GET['aid'];
}
else if (isValid($_SESSION['aid']))
{
    $affiliationID = trim($_SESSION'aid'];
}
else if (isValid($_COOKIE['aid']))
{
    $affiliationID = trim($_COOKIE['aid'];
}
else
{
    throw new Exception('No affiliation ID defined');
}

function isValid($affiliationID)
{
    if (empty($affiliationID))
    {
        return false;
    }
    else
    {
        return true;
    }
}
  1. Why would you test for session and cookie, in case you have a valid affiliateID from the $_GET array? ==> Make it progressive so that session is only checked, if no GET was found and cookie is only checked if no session was found.

  2. Don't repeat the validation of the affiliateID. ==> Write a validate function and reuse it, you might want to add more rules later on.

  3. Use curly brackets to make your code more readable

  4. $aid or $aff are BAD variable names, $affiliateID instead is a GOOD one! You don't win anything for writing short variables names but you win a lot with writing self-explanatory code.

Bad example, doesn't talk

if (validate($aff)) 

Good example, talks to you

if (isValid($affiliationID))

So my proposal for change of the core components:

if (isValid($_GET['aid']))
{
    $affiliationID = trim($_GET['aid'];
}
else if (isValid($_SESSION['aid']))
{
    $affiliationID = trim($_SESSION'aid'];
}
else if (isValid($_COOKIE['aid']))
{
    $affiliationID = trim($_COOKIE['aid'];
}
else
{
    throw new Exception('No affiliation ID defined');
}

function isValid($affiliationID)
{
    if (empty($affiliationID))
    {
        return false;
    }
    else
    {
        return true;
    }
}
骄兵必败 2024-08-24 18:33:00

谢谢大家,这看起来越来越好。可能需要向您澄清的一点是,如果在 GET 中给出了 aff id,那么它必须是在我们可能清除其他人的 aff id 之前存在的有效 ID。每笔交易都涉及金钱,我们希望附属机构能够尽可能长时间地获得信用。

对于 empty 它不是太有用,因为空格会欺骗它。所以除非你在使用之前进行修剪,否则我觉得它不够准确。所以我不知道 GET 的空值。对于其他人来说没关系,因为我们已经检查过它们。

这是到目前为止我从你的帮助中得到的信息(这里的复杂三元组在发现 true 时是否会中断?我不希望它继续执行该行):

session_start(); // start session

  $aid = !empty($_GET['aid'])     ? trim($_GET['aid']) :
         !empty($_SESSION['aid']) ? $_SESSION['aid'] : 
         !empty($_COOKIE['aid'])  ? $_COOKIE['aid'] :
         '';

  // use first key from array if aid not set
  if(!isset($a[$aid])) $aid = key($a);

  if(!isset($_SESSION['aid']) || $aid != $_SESSION['aid'])
  {
    setcookie('aid', $aid);
    $_SESSION['aid'] = $aid;
  }

Thanks guys this is looking better and better. One point that might clarify for you, is that IF an aff id is given in GET it MUST be a valid one that exists before we possibly wipe out someone else's aff id. Money is involved with each transaction and we want an affiliate to get credit for as long as possible.

Regarding empty it's not too useful since whitespace fools it. So unless you trim before using it, I feel it's not accurate enough. So I don't know about the empty for the GET. It's ok for the others because we've already checked them.

Here's what I have so far from your help (does the complex ternary here break when it finds true? I don't want it to keep executing the line):

session_start(); // start session

  $aid = !empty($_GET['aid'])     ? trim($_GET['aid']) :
         !empty($_SESSION['aid']) ? $_SESSION['aid'] : 
         !empty($_COOKIE['aid'])  ? $_COOKIE['aid'] :
         '';

  // use first key from array if aid not set
  if(!isset($a[$aid])) $aid = key($a);

  if(!isset($_SESSION['aid']) || $aid != $_SESSION['aid'])
  {
    setcookie('aid', $aid);
    $_SESSION['aid'] = $aid;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文