Answer the question
In order to leave comments, you need to log in
How to enable/disable following a link from a specific page?
Hi friends!
Task: It is necessary to allow visiting the target page A only by clicking on the links located on a specific page B. That is, so that by a direct link in the browser line, a link located outside of a specific page B, it was impossible to go to page A, but to do, for example, a redirect back or to the main page or something else - for example, connect your action.
There are thoughts on how to implement this, but I would like to see the correct way. Friends, how to drink it?
Answer the question
In order to leave comments, you need to log in
When a link is clicked, write isLisnkClicked = 1 to the session. Then
/*
тут храняться как страницы, те от которых зависит показ какой то страницы,
так и зависимые страницы. name это /module/controller/action
*/
Good afternoon.
Check which page you came from and if it matches, then redirect where you need it.
You can do such a check in beforeAction.
When entering page B, write a variable to the cookie or session, when going to page A, check for the presence of this variable.
Friends, and so:
In parallel, I posted here yiiframework.ru/forum
Two weeks have passed, and here and there there is no unequivocal opinion on how to implement this functionality. I solved the issue like this:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class MainController extends Controller
{
public function init()
{
parent::init();
$this->__TOKEN();
}
public function mainToken( $token = null )
{
$session = $this->openSession();
if ( isset($token) )
{
$_SESSION['__TOKEN'][0] = $token;
$_SESSION['__TOKEN'][1] = 0;
}
return $_SESSION['__TOKEN'][0];
}
public function openSession()
{
$session = Yii::$app->session;
if ( !$session->isActive ) $session->open();
return $session;
}
protected function __TOKEN()
{
$session = $this->openSession();
isset ($_SESSION['__TOKEN'][1] ) ? $_SESSION['__TOKEN'][1]++ : $_SESSION['__TOKEN'][1] = 0;
if ( $_SESSION['__TOKEN'][1] > 1 )
{
$_SESSION['__TOKEN'][0] = null;
$_SESSION['__TOKEN'][1] = 0;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question