如何在<a>

时间:2016-03-15 05:25:12

标签: php html session onclick

I have a class named User which has a function named logout(). I create an instance of this class in index.php and i pass it's value to $_SESSION[usr] before i call memberspage.php . In memberspage.php i have a link named logout which when clicked i want the logout() function to run and also send the user to index.php. For this purpose i've done something like this.

<a href="index.php" onclick= "$_SESSION[usr]->logout()">Log out</a>

I know that -> causes the problem but i don't know how to fix it. thnx for your time.

The following code worked for me

<a href="index.php" onclick ="alert('<?php echo $_SESSION["user"]->logOut(); ?>')">Log out</a>

but there is a problem. If i go to the page(memberspage.php) where the above code is and i press the back arrow (not logout link) the logOut() function will still be used(the session is destroyed and i will have to log in again to access memberpage.php) . I don't get it because i thought that the only way to call the logOut() function was to click on Log out link.

3 个答案:

答案 0 :(得分:1)

首先,我建议您更改会话的使用,您可以创建一个页面,例如session.php,其中包含所有会话,它也可以是登录页面的重新目录页面。

就像这个名为login.php

的人

在表单中创建make action重定向到session.php

我还建议您登录的所有PHP代码都在session.php中,然后再创建一个。

   <?php

session_start();

$host = "localhost";
$uname = "root";
$pass = "";
$db = "mydb;

//database connection
$conn = mysqli_connect($host, $uname, $pass, $db);
mysqli_select_db($conn, $db);

if(!$conn){
    die("Connection failed: " . mysqli_connect_error());
}


if(isset($_POST['username'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $username = stripslashes($username);
    $password = stripslashes($password);
    //$username = mysqli_real_escape_string($username);
    //$password = mysqli_real_escape_string($password);

    $sql = "SELECT * FROM table WHERE username = '" .$username. "' AND password = '".$password."' LIMIT 1";
    $res = mysqli_query($conn, $sql);
    if(mysqli_num_rows($res) > 0){
        if($data = mysqli_fetch_assoc($res))
            {
                $_SESSION['type'] = $data['type'];


          if(isset($_SESSION["login_user"]))
 {

                    if($data['type'] == 'admin'){
                        header('location: admin.php');
                    }

                  else  if($data['type'] == 'customer'){
                        header('location: customerhome.php');
                    }//header('location: uservalidation.php');




 }

            }
        }
    else{
//header('location: #');



        echo '<script>';
        echo 'alert("Invalid no?")';

        echo '</script>';

          header('location: logind.php');

    }

}


?>

然后创建另一个页面logout.php

将此代码放入:

  <?php

session_start();
header('location: index.php');
session_destroy();


?>

然后保存将你的页面链接为logout.php

答案 1 :(得分:1)

如果您在评论中说$_SESSION[usr]->logout()正在为您工作。我不知道怎么做。

但这里只是在锚标签中调用一个php函数。它完全取决于你的函数响应。

    <?php
        function usr(){
            return "abc";
        }

    ?>

    <a href="index.php" onclick ="alert('<?php echo usr(); ?>')">Log out</a>

答案 2 :(得分:1)

添加文件logout.php并将退出实施输入其中:

<?php

header('Content-Type: application/json');

$_SESSION[usr]->logout();

echo json_encode(['message' => 'ok']);

用AJAX调用此文件:

<script>
function logout() {
    $.ajax({
        url: '/logout.php'
    }).then(function (res) {
        window.location.href = '/';
    });
}
</script>

<a href="#" onclick= "logout();">Log out</a>