有人可以幫我清理這個,並使其更合乎邏輯嗎?我現在炒,似乎無法得到一個很好的代碼行:)更好地捕獲會話從PHP中的會員代碼ID
我試圖從網址捕捉會員ID像?aid = 3056677。這個想法是IF aff id在GET中被設置,優先,會話,最後是cookie。另外,我們不希望設置不存在的aff標識。
你知道更多的嘗試和做法嗎?
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;
我做了修改利用'isValid'函數確保您的值實際上是有效的。三元運算符一旦找到有效變量就會中斷。 – 2010-01-14 01:08:32