2012-09-21 64 views
-1

我有2個片段(下面)。問題是如果statemnt if ($loggedin != NULL){在第二個片段中不通過,即使該變量不爲null。它像第一個片段中的$loggedin變量不適用於它。如果我將2個片段合併爲1,則它們工作正常。ModX:同一頁上的2個片段互相交互

有誰知道如何讓2個片段'對話'對方? (PS,運行雷沃2.1.3)

<?php 
$loggedin = ""; 
if (!isset($_SESSION['user_id'])) { 
// redirect to login page 
} 
else { 
$loggedin = "true"; 
} 

第二:

<?php 
if ($loggedin != NULL){ 
echo "logged in"; 
} 
else { 
echo "error"; 
} 
+0

你可以只檢查'isset($ _ SESSION [ 'user_id']' – OptimusCrime

回答

1

首先,你不能傳遞$的loggedIn變量到第二片斷,爲此常與後或會話變量。

其次,是檢查一個簡單的方法,這些都是直出Bob的指南:

There are various methods. One easy method is to use this code: 

if ($modx->user->get('username') == '(anonymous)') { 
    /* user is not logged in */ 
}  
Here is the official method for seeing if the user is logged in 
to the current context: 

if ($modx->user->hasSessionContext($modx->context->get('key'))) { 
    /* user is logged in */ 
}  
If you know the name of the current context (e.g., web), 
you can use this method. The name of the context is required: 

if $modx->user->isAuthenticated('web') { 
    /* user is logged in to web context */ 
} 

也就是說,如果你需要推出自己的身份驗證的某些原因。 〜否則,登錄/註冊額外將爲你做這一切。

* UPDATE ***從一個片段 兩個變量傳遞到另一個在相同的資源,你可以設置/獲取佔位符:

<?php 
// snippet one 

$modx->setPlaceholder('output','Place holder set!'); 



<?php 
// snippet two 

$myvar = $modx->getPlaceholder('output'); 

echo 'this is the value of "output": '.$myvar; 
+0

謝謝Sean,我明白如何通過帖子或會話來傳遞var,但這兩個片段在一個接一個的頁面上,我必須將var傳遞給第二個片段一個參數?例如:[[snippet&parameter ='$ loggedin']] – MeltingDog

+0

Ahhh - 好吧,看看我的編輯 –

+0

啊!感謝堆 – MeltingDog