简体   繁体   English

仅在登录后重定向用户

[英]Redirect user only after login

I have a search input to search for the required subject. 我有一个搜索输入来搜索所需的主题。 I am able to display the appropriate result, I want the result to be displayed but user can only to go to the link if he logins, else display message box "You have to login to be able to go to another page". 我能够显示适当的结果,我希望显示结果,但是用户只有在登录后才能转到链接,否则显示消息框“您必须登录才能转到另一个页面”。 The problem here is i can't seem to display the error message at all. 这里的问题是我似乎根本无法显示错误消息。 I want to display the result even if user is not or is logged in. Just that directing him to another page display error message and he must login or he cant be directed 即使用户不在或未登录,我也要显示结果。只是将用户定向到另一页会显示错误消息,并且必须登录或无法定向

<form class="search">
                <div class="form-group input">
                <div class="icon-addon">
                <input type="text" name="type" class="st-search" id="search" placeholder="Search"  autocomplete="off" >
                <label for="email" class="glyphicon glyphicon-search" rel="tooltip" title="email"></label>
                </div>
                <h4 id="results-text">Showing: <b id="search-string">Output</b></h4>
                <ul id="results"></ul>                                  
        </div>      
</form>

find.php find.php

<?php
$dbhost = "hostname";
$dbname = "databasename";
$dbuser = "root";
$dbpass = "";

global $task_db;

$task_db = new mysqli();
$task_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$task_db->set_charset("utf8");

if ($task_db->connect_errno) {
    printf("You can't connect: %s\n", $task_db->connect_error);
    exit();
}
    session_start();
    $_SESSION['login'] = true;
    // If user is not logged in
    if(!$_SESSION['login']){
        $message = 'You are not logged in';
        echo $message;
   die;
    }
//if user is logged in, user can be redirected to the link. Something is not right here
else{
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_blank" href="url">';
$html .= '<h3>name</h3>';

$html .= '</a>';
$html .= '</li>';

$search = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search = $task_db->real_escape_string($search);

if (strlen($search) >= 1 && $search !== ' ') {
    $query = 'SELECT * FROM tablename WHERE columnname LIKE "%'.$search_string.'%"';
    $result = $task_db->query($query) or trigger_error($task_db->error."[$query]");
    while($results = $result->fetch_array()) {
        $result_array[] = $results;
    }
    if (isset($result_array)) {
        foreach ($result_array as $result) {
            $name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search."</b>", $result['subject_Name']);
            //display url
            $url = 'https://www.google.com/'.urlencode($result['subject_Name']).'&lang=en';
            $output = str_replace('name', $name, $html);
            $output = str_replace('url', $url, $output);
            echo($output);
        }
    }else{
        // No Results found
    }
}
}
?>

First session_start(); 第一个session_start(); shoul be the first command Second 应该是第一条命令

 $_SESSION['login'] = true;

sets $_SESSION['login'] allway to true so that 将$ _SESSION ['login']始终设置为true,以便

if(!$_SESSION['login']){

will never happen. 永远不会发生。

Yes, because you set $_SESSION['login'] = true; 是的,因为您设置了$_SESSION['login'] = true;
so the check if (!$_SESSION['login']){ will never be true. 因此检查if (!$_SESSION['login']){永远不会为真。

You always get the result; 您总是得到结果;

//$_SESSION['login'] = true;

When your user login set the $_SESSION['login'] = true; 当您的用户登录名设置$_SESSION['login'] = true; but not just before the if (!$_SESSION['login']){ line 但不仅在if (!$_SESSION['login']){行之前

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

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