M
M
Mark Dio2016-10-14 16:11:54
Yii
Mark Dio, 2016-10-14 16:11:54

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

4 answer(s)
P
Pavel Tereshchenko, 2016-10-14
@l2ping

When a link is clicked, write isLisnkClicked = 1 to the session. Then

/* 
тут храняться как страницы, те от которых зависит показ какой то страницы, 
так и зависимые страницы. name это /module/controller/action 
*/

user_available_pages table to see if the page is available to the user.
You can also check for cookies.

D
Dmitry, 2016-10-14
@slo_nik

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.

Z
Zhainar, 2016-10-14
@zhainar

When entering page B, write a variable to the cookie or session, when going to page A, check for the presence of this variable.

M
Mark Dio, 2016-10-25
@Touranchoks

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;
        }    
    }
}

$this->mainToken() - read the token;
$this->mainToken('string') - write the token;
The life cycle of mainToken is 1 request, whether we have read the token or not, the token will be cleared.
From MainController, you can extend and build further application logic. Something like this...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question