当我从另一个页面重定向时

当我从另一个页面重定向时

本文介绍了当我从另一个页面重定向时,PHP 会话检查不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个密码检查功能,它设置会话变量并重定向到 dashboard.php

Dashbord.php 像这样检查会话变量

在仪表板中,我有几个指向 php 文件的链接,这些文件使用相同的东西来检查会话.

但是当我点击仪表板链接时,我被重定向到登录页面.但是如果我手动导航到链接,我可以访问该页面.我尝试使用以下方法清除会话:

并尝试手动进入页面,我被重定向到登录,所以代码没问题.但我需要手动从仪表板导航到工作.我做错了什么.

add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加产品类别</title><link rel="stylesheet" type="text/css" href="view.css" media="all"><script type="text/javascript" src="view.js"></script><脚本>函数validateForm(){var x = document.forms["form_974780"]["productname"].value;if (x == null || x == "") {alert("请输入新产品名称");返回假;}}</头<body id="main_body" ><?phpif (isset($_POST['submit'])){$productname=$_POST['productname'];//conn 到这里$check=$conn->query("SELECT pname FROM products WHERE pname='$productname'");$num_rows = mysqli_num_rows($check);如果($num_rows>0){echo "";echo("经销商已经存在".$productname);回声"</font>";}别的{$result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");如果($结果){echo "";echo("插入成功".$productname);回声"</font>";}别的{echo "";echo("插入时出错");回声"</font>";}}mysqli_close($conn);}别的{}?><img id="top" src="top.png" alt=""><div id="form_container"><h1><a>添加产品类别</a></h1><form id="form_974780" class="appnitro" method="post" action="?"onsubmit="返回validateForm()"><div class="form_description"><h2>添加产品类别</h2><p></p>

<ul ><li id="li_12" ><label class="description" for="element_12">现有产品类别</label><div><select class="element select large" id="element_12" name="element_12"><?php//conn 到这里$result=$conn->query("SELECT pname FROM products");而 ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){echo "<option value='".$row['pname']."'>".$row['pname']."</option>";}mysqli_close($conn);?></选择>

<li id="li_13" ><label class="description" for="element_13">添加新类别</label><div><input id="element_13" name="productname" class="element text large" type="text" maxlength="350" value=""/></div><p class="guidelines" id="guide_13"><small>输入要添加的新产品类别,然后单击提交按钮.</小></p><li class="buttons"><input type="hidden" name="form_id" value="974780"/><input id="saveForm" class="button_text" type="submit" name="submit" value="submit"/></表单>

<img id="bottom" src="bottom.png" alt=""></html>

解决方案

您只是检查是否设置了 $user_check ..您还应该检查其中的值.

检查类似的东西.

if(!isset($user_check) 或 $user_check==""){header("位置:http://www.none.com/dashboard/login.php");}

现在它检查它的值.否则,请避免在检查条件之前为 $user_check 分配值.做这样的事情.

if(!isset($_SESSION['login_user'])){header("位置:http://www.none.com/dashboard/login.php");}

最后你的代码应该是这样的..

希望这有帮助..

I have a password check function which sets the session variable and redirects to dashboard.php

<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];




if($myusername=="admin" && $mypassword=="admin")
{

$_SESSION['login_user']=$myusername;
header("location: dashboard.php");
}
else
{
echo "Your Login Name or Password is invalid";
}
}
?>

Dashbord.php checks for the session variable like this

<?php
    session_start();
    $user_check=$_SESSION['login_user'];
    if(!isset($user_check))
    {
       header("Location: http://www.none.com/dashboard/login.php");
    }
?>

In the dashboard I have several links to php files that employs the same thing to check for session.

<?php
    session_start();
    $user_check=$_SESSION['login_user'];
    if(!isset($user_check))
    {
       header("Location: http://www.none.com/dashboard/login.php");
    }
?>

But when i click on the dashboard links,i get redirected to the login page. But if i manually navigate to the link, I can access the page.I tried clearing the session using:

<?php
session_start();
unset($_SESSION['login_user']);
echo "Logged out of Dashboard";
?>

and tried manually entering the page, I was redirected to login, so the code is okay. But I need to manually navigate from dashboard to work. What am I doing wrong.

add.php

<?php
       session_start();
        if(!isset($_SESSION['login_user']))
         {
          header("Location: http://www.none.com/dashboard/login.php");
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add Product Category</title>
    <link rel="stylesheet" type="text/css" href="view.css" media="all">
    <script type="text/javascript" src="view.js"></script>
    <script>
    function validateForm() {
        var x = document.forms["form_974780"]["productname"].value;
        if (x == null || x == "") {
            alert("Please Enter New Product Name");
            return false;
        }
    }
    </script>
    </head
    <body id="main_body" >
    <?php
    if (isset($_POST['submit']))
    {
    $productname=$_POST['productname'];

   //conn goes here
    $check=$conn->query("SELECT pname FROM products WHERE pname='$productname'");
    $num_rows = mysqli_num_rows($check);
    if($num_rows>0)
    {

    echo "<font color=\"white\">";
    echo("Dealer Already exists ".$productname);
    echo"</font>";
    }
    else
    {
    $result=$conn->query("INSERT INTO products(pname)VALUES('$productname')");
    if($result)
    {
    echo "<font color=\"white\">";
    echo("Successfully Inserted ".$productname);
    echo"</font>";
    }
    else
    {
    echo "<font color=\"red\">";
    echo("Error when inserting");
    echo"</font>";
    }
    }
    mysqli_close($conn);
    }
    else
    {

    }
    ?>
        <img id="top" src="top.png" alt="">
        <div id="form_container">

            <h1><a>Add Product Category</a></h1>
            <form id="form_974780" class="appnitro"  method="post" action="?" onsubmit="return validateForm()">
                        <div class="form_description">
                <h2>Add Product Category</h2>
                <p></p>
            </div>
                <ul >

                        <li id="li_12" >
            <label class="description" for="element_12">Existing Product Categories  </label>
            <div>
            <select class="element select large" id="element_12" name="element_12">
    <?php
    //conn goes here
    $result=$conn->query("SELECT pname FROM products");
    while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
    echo "<option value='".$row['pname']."'>".$row['pname']."</option>";
    }
    mysqli_close($conn);
    ?>

            </select>
            </div>
            </li>       <li id="li_13" >
            <label class="description" for="element_13">Add New Category </label>
            <div>
                <input id="element_13" name="productname" class="element text large" type="text" maxlength="350" value=""/>
            </div><p class="guidelines" id="guide_13"><small>Enter the new Product Category to add and Click the Submit Button. </small></p>
            </li>

                        <li class="buttons">
                    <input type="hidden" name="form_id" value="974780" />

                    <input id="saveForm" class="button_text" type="submit" name="submit" value="submit"/>
            </li>
                </ul>
            </form>

        </div>
        <img id="bottom" src="bottom.png" alt="">
        </body>
    </html>
解决方案

You are just checking whether $user_check is set or not..You should also check for the value in it.

Check something like this.

if(!isset($user_check) or $user_check=="")
{
   header("Location: http://www.none.com/dashboard/login.php");
}

Now it checks for the value of it.Otherwise, avoid assigning a value for $user_check before checking the condition. Just do something like this.

if(!isset($_SESSION['login_user']))
{
   header("Location: http://www.none.com/dashboard/login.php");
}

Finally your code should look like this..

<?php
session_start();
if(!isset($_SESSION['login_user']))
{
   header("Location: http://www.none.com/dashboard/login.php");
}
?>

Hope this helps..

这篇关于当我从另一个页面重定向时,PHP 会话检查不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:37