简体   繁体   English

如果用户未登录,则 PHP 重定向 - Yii2

[英]PHP redirect if user not log in - Yii2

My site sends email with a link to the user if the CO is finalized.如果 CO 最终确定,我的站点会向用户发送带有链接的电子邮件。

If the user is not signed in the user must first go to the login page, and only if user is signed in then they are redirected to the url.如果用户未登录,则用户必须首先转到登录页面,并且只有当用户登录时,他们才会被重定向到 url。

Now if user is signed in they go to directly to the url, but not signed in go to log in page, he can log in but he is not direct to the url can anyone help with that?现在,如果用户已登录,他们会直接转到 url,但未登录会转到登录页面,他可以登录,但不能直接访问 url 任何人都可以帮忙吗?

this page url with some parameters is send to the user to as link这个带有一些参数的页面 url 作为链接发送给用户

<?php
if(isset($_SESSION['ssid']) &&  $_SESSION['ssid'] == session_id() ) 
{ 
      // do you staff, after login confirm
}          
else { 
    // redirect to loginPage.php
}
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use kartik\grid\GridView;
use backend\models\Item;
use backend\models\Respond;
use kartik\time\TimePicker;
use backend\models\RespondItem;
use backend\models\RequestItem;
use backend\models\DistributorHasItemCode;
?>
<?php
$check = null;
$checknow = null;
$checkfield = null;
$today = date('Y-m-d');
$date = date("Y-m-d H:i:s", time()); 
$date1 = new DateTime($date); 
$date2 = new DateTime($model->end_date.' '.$model->end_time);
if(empty($model->end_date))
{
    $model->end_date = $today;
}

if (Yii::$app->user->identity->ref_table =="sales_rep") 
{
    $sales_rep = Yii::$app->user->identity->ref_id;
    $respond = Respond::find()->where(['sales_rep_id'=>$sales_rep,'customer_order_request_id'=>$model->id])->one();

    if (!empty($respond) && $respond->status == 'Finalized and customer Notified' || $date1>=$date2 ) 
    {
        $check = "disabled='disabled'";
    }

    if ($model->status == 'Finalized and Sales Rep Notified') {
        $checknow = "disabled='disabled'";

    }

}else {
    if ($model->status == 'Finalized and Sales Rep Notified') {
        $check = "disabled='disabled'";
    }
}
?>
  <?= Yii::$app->session->getFlash('success') ?>
  <div class="customer-order-request-form">

<?php $form = ActiveForm::begin(['id'=>'cor']); ?> 

<fieldset <?= $check?> >
<fieldset <?= $checknow?> >
<div class="row">
    <div class="col-lg-4">

        <?= $form->field($model, 'code')->textInput(['maxlength' => 10,'readonly'=>TRUE]) ?>
    </div>
    <div class="col-lg-2" disabled>
    <?= $form->field($model, 'end_date')->widget(\yii\jui\DatePicker::classname(), [
            'dateFormat' => 'yyyy-MM-dd',
            'clientOptions'=> ['defaultDate'=>$today,'minDate' =>$today,],
            'options'=>[
                'class'=> 'form-control',

            ],
            ])
    ?>
    </div>
    <div class="col-lg-2">
        <?= $form->field($model, 'end_time')->widget(TimePicker::classname(), [
            'pluginOptions' =>
            [
                'showMeridian' => true,
            ],

            ]);
        ?>
    </div>

    <div class="col-lg-4">
        <?= $form->field($model, 'status')->textInput(['readonly'=>TRUE]) ?>

    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <?= $form->field($model, 'description')->textarea(['rows'=>3]);?>
    </div>
</div>
</fieldset>
<div class="form-group">
    <?= Html::submitButton((Yii::$app->user->identity->ref_table =="sales_rep")?'Save':'Update', ['class' =>'btn btn-success']) ?>
    <?= Html::button((Yii::$app->user->identity->ref_table =="sales_rep")?'Finalize & Notify Customer':'Finalize & Notify Sales Reps', ['class' => 'btn btn-primary finalize']) ?>
</div>

</fieldset>
<?php ActiveForm::end(); ?>

Should use session to handle login.应该使用会话来处理登录。

Suppose your url is http://somedomain.com/somepage.php?token=some_tokan .假设您的网址是http://somedomain.com/somepage.php?token=some_tokan So, at somepage.php , use session checking for login confirm like :因此,在somepage.php ,使用会话检查进行登录确认,例如:

<?php 

   ob_start();
   session_start();  

   if(isset($_SESSION['ssid']) &&  $_SESSION['ssid'] == session_id() ) 
   { 
          // do your stuff, after login confirm
   }          
   else { 
        // redirect to loginPage.php
        // With "header('Location:yoururl'); for example
   }

And in loginPage.php , after successful login credentials, assign $_SESSION['ssid'] value like:loginPage.php 中,成功登录凭据后,分配$_SESSION['ssid']值,如:

    ob_start();
    session_start();  
    //  if login credentials are verified.
    $_SESSION['ssid'] = session_id();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM