页面重定向到自己

时间:2016-10-09 16:27:14

标签: php mongodb

我正在尝试为登录页面添加用户,但即使代码中没有提供重定向,它也会保持重定向

的login.php

<?php
include './connect.php';
session_start();
if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST['username'])) && (!empty($_POST['password']))) {
    $postedUsername = $_POST['username'];
    $postedPassword = $_POST['password'];
    $userDatabaseFind = $database->login->findOne(array('username' => $postedUsername, 'password' => $postedPassword));
    $storedUsername = $userDatabaseFind['username'];
    $storedPassword = $userDatabaseFind['password'];

    if (($postedUsername == $storedUsername) && ($postedPassword == $storedPassword)) {
        $_SESSION['username'] = $storedUsername;


        if($_SESSION['username'] == 'admin'){

            session_regenerate_id();
             header('location:printDetail.php');
            }
        else{
                session_regenerate_id();
                header('location:welcome.php');
        }

    } else {
        echo 'Error';
    }
}

printDetail.php

<?php
require './connect.php';
require './login.php';

//SEARCHING ACROSS THE COLLECTIONS IN THE DATABASE

$printDetailCollections = $database->details;
$printDetailCursor = $printDetailCollections->find();

//INITIALIZE THE VALUE TO ZERO SO IT CAN GO THROUGH THE DATABASE

$i = 0;

//USERNAME AND PASSWORD ARE CHECKED IF IT IS ASSIGNRD THEN THE SESSION IS DISPLAYED

if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {

    //LOOP FOR TRAVERSING ACROSS THE DATABASE

    foreach ($printDetailCursor as $doc) {
        $i++;
    }
} else {
    header('location:index.html');
}
?>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <style>
            table, th, td {
                border: 1px solid black;
                border-collapse: collapse;
            }
            th, td {
                padding: 15px;
            }
            body{
                background-image: url(images/16386858141_65a65879cd_b.jpg);
                background-repeat: no-repeat;
                background-size: cover;

            }
        </style>
        <script>
            $(document).ready(function () {
                window.onload = $('#table').hide();
            });

        </script>
    </head>

    <body>
        <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="#">Business card Management</a>
                </div>
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu
                            <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="addUser.php">Add entry</a></li>
                            <li><a href="#">Delete entry</a></li>
                        </ul>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> Log out</a></li>
                </ul>
            </div>

        </nav>
        <div class="container">
            <table id="table">
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Second Name</th>
                        <th>Company Name</th>
                        <th>Designation</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($printDetailCursor as $printDetailDocument) {
                        ?>
                        <tr>
                            <td>
                                <?php
                                echo (json_decode($printDetailDocument['First Name']));
                                ?>
                            </td>
                            <td>
                                <?php
                                echo (json_decode($printDetailDocument['Second Name']));
                                ?>
                            </td>
                            <td>
                                <?php
                                echo (json_decode($printDetailDocument['Company Name']));
                                ?>
                            </td>
                            <td>
                                <?php
                                echo (json_decode($printDetailDocument['Designation']));
                            }
                            ?>
                        </td>
                    </tr>
                </tbody>

            </table>

        </div>
    </body>
</html>

我在mongodb登录和详细信息中有3个集合。添加用户尝试访问登录集合以添加用户

addUser.php

<?php

    include './connect.php';
$username = NULL;
$password = NULL;
$confPassword = NULL;
$passwordError = "";
$userEntryCollection = NULL;

if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $userEntryCollection = $database->login;
        if (isset($_POST['username'])) {
            $username = $_POST['username'];
        }
        if (isset($_POST['password'])) {
            $password = $_POST['password'];
        }
        if (isset($_POST['confPassword'])) {
            $confPassword = $_POST['confPassword'];
        }
    } else {
        echo 'error';
    }
    if ($password != $confPassword) {
        $passwordError = "Your passwords doesnot match";
    } else {
        $userEntry = array(
            "username" => $username,
            "password" => $password,
        );
        $userEntryCollection->insert($userEntry);
    }
}
?>

<html>
    <head>
        <link rel="stylesheet" href=bootstrap-3.3.7-dist/css/bootstrap.min.css>
        <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
        <style>
            .col-lg-10{
                position: relative;
                width: 350px;
                border-radius: 25px;

            }
            form{
                position: absolute;
                top: 10%;
                left: 40%;
            }
            body{
                background-image: url(images/16386858141_65a65879cd_b.jpg);
                background-repeat: no-repeat;
                background-size: cover;
            }
            form{
                color: black;
            } 

        </style>

    </head>
    <body>
        <nav class="navbar navbar-inverse">
            <ul class="nav navbar-nav navbar-left">
                <li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
            </ul>
        </nav>
        <form  method="POST" class="col-lg-10" <?php echo $passwordError; ?> >
            <div class="form-group form-inline"><br>
                <label for="firstName">Username</label><br>
                <input type="text" class="form-control" id="username" name="username">
            </div>
                <div class="form-group form-inline">
                <label for="secondName">Password:</label><br>
                <input type=password class="form-control" id="password" name="password">
                </div>
                <div class="form-group form-inline">
                <label for="secondName">Confirm Password</label><br>
                <input type=password class="form-control" id="confPassword" name="confPassword">
                </div>
                <button type="button" class="btn btn-success">Submit</button>
        </form>
    </body>
</html>

我试图通过URL禁用对printDetail.php的访问 我认为这是重定向的主要原因。但是无法找出代码中的错误

只需点击用户名

字段即可重定向网页

P.s:我知道以这种方式存储的密码是不安全的

1 个答案:

答案 0 :(得分:0)

我在我的服务器上尝试了您的代码,并在尝试以addUser.php形式输入内容时确认了页面重定向问题...

你的错误在于以下几点:

<nav class="navbar navbar-inverse">
    <ul class="nav navbar-nav navbar-left">
        <li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
    </ul>
</nav>

你没有为#34;关闭&#34;关闭ancor。

结果是你的所有形式和页面的每个元素(直到另一个ancor ......但没有)被浏览器包含在这个ancor中。
然后也是&#34;返回&# 34;链接。 enter image description here 这很简单。

使用[F12]通过控制台和代码检查器进行调试,下次...
;)