简体   繁体   中英

Processwire - PHP - if link is clicked

I have a button on my page that if clicked opens a modal that allows user to create a topic. I want to implement some checks, if user if logged in allow to create IF not then forward to login page. AS testing I have made IF logged in > forward to "/" IF not then for to "/login"

My code is currently as following, which does not work. It always allows to create.

//Check if user is logged in before allowing to create
if (isset($_GET['create'])) { 

    //Check if current user is logged in 
    if ($user->isLoggedin()) { 

        $session->redirect("/");

    }

           //If current use not logged in redirect to login
    else {

        $session->redirect("/login"); 
    }
}
<a href="#?create" role="button" name="create" class="btn btn-success btn-block" data-toggle="modal" data-target="#basicModal">

You have have to check session before enter into create code.

session_start();
if(!isset($_SESSION['sess_user_id']) || (trim($_SESSION['sess_user_id']) == '')) {
header("location: login.php");
}

It's hard to answer this for sure without knowing what you are passing in the url to this page. You could have ?create=&somethingelse=true and the isset for create would still be true.

Also, in Processwire you can use:

$input->get->create

If you can provide more information on the url that is being used to access this page, I am sure it will be easy to fix. Is it the url that is coming from the create button code at the end of your code block?

Is this code in a PW template php file, or is it a bootstrapped script?

Something else that is confusing about your code is the href line at the end - it is html? You never closed your php tags from the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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