我在简单地输入注册页面时收到此错误
Undefined index: invite
但当我使用进入同一页面时
url?invite=2000 this error is not shown...
下面代码的第二行显示了错误。
$mySess = JFactory::getSession();
$_SESSION['fromid'] = $_GET['invite'];
$fromid = $_SESSION['fromid'];
如果url中没有使用"invite",我如何初始化它。。。
使用isset
函数,如下所示:
$mySess = JFactory::getSession();
$_SESSION['fromid'] = isset($_GET['invite']) ? $_GET['invite'] : '';
$fromid = $_SESSION['fromid'];
这基本上是Matt的解决方案,但没有复杂的if-then-else
简写。
$mySess = JFactory::getSession();
// this value will be used if you did not pass the parameter invite to your script
$inviteDefaultValue = -1;
// isset checks if the variable exists.
// you can also use array_key_exists("invite", $_GET)
if (isset($_GET['invite']))
{
// $_GET['invite'] is set, so we can use it
$_SESSION['fromid'] = $_GET['invite']);
}
else
{
// $_GET['invite'] is not set, so use your default value here
$_SESSION['fromid'] = $inviteDefaultValue;
}
$fromid = $_SESSION['fromid'];
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/function.array-key-exists.php