在 PHP 中更好地从 GET 捕获会话会员 ID
有人可以帮我清理这个并使其更符合逻辑吗?我现在很困惑,似乎写不出一行好的代码:)
我正在尝试从像 ?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您从 $_GET 数组中获得了有效的会员 ID,为什么要测试会话和 cookie? ==> 使其渐进式,以便仅在未找到 GET 时检查会话,仅在未找到会话时检查 cookie。
不要重复验证affiliateID。 ==> 编写一个验证函数并重用它,稍后您可能需要添加更多规则。
使用大括号使您的代码更具可读性
$aid 或 $aff 是不好的变量名称,而 $affiliateID 是一个好的变量名称!编写简短的变量名称不会带来任何好处,但编写不言自明的代码会带来很多好处。
坏示例,不说话
好示例,与你交谈
所以我建议更改核心组件:
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.
Don't repeat the validation of the affiliateID. ==> Write a validate function and reuse it, you might want to add more rules later on.
Use curly brackets to make your code more readable
$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
Good example, talks to you
So my proposal for change of the core components:
谢谢大家,这看起来越来越好。可能需要向您澄清的一点是,如果在 GET 中给出了 aff id,那么它必须是在我们可能清除其他人的 aff id 之前存在的有效 ID。每笔交易都涉及金钱,我们希望附属机构能够尽可能长时间地获得信用。
对于
empty
它不是太有用,因为空格会欺骗它。所以除非你在使用之前进行修剪,否则我觉得它不够准确。所以我不知道 GET 的空值。对于其他人来说没关系,因为我们已经检查过它们。这是到目前为止我从你的帮助中得到的信息(这里的复杂三元组在发现 true 时是否会中断?我不希望它继续执行该行):
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):