Code Monkey home page Code Monkey logo

Comments (2)

sigee avatar sigee commented on July 29, 2024 3

@crichardson9,
Maybe there is a better way, but you can check the $_SERVER['REQUEST_URI']

$router->before('GET', '/.*', function() {
    if ($_SERVER['REQUEST_URI'] != '/login' && !isset($_SESSION['user'])) {
        header('location: /login');
        exit();
    }
});

from router.

bramus avatar bramus commented on July 29, 2024 2

In addition to @sigee's answer – which does the job just fine – you could also make use of the fact that the parts of dynamic routes can be converted to variables/parameters in the callback (see Route Patterns)

$router->before('GET', '/(.*)', function($path) {
    if ($path !== 'login' && !isset($_SESSION['user'])) {
        header('location: /login');
        exit();
    }
});

Additionally it might be a good idea to not check for equality, but to check if $path starts with login or not.

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.