简体   繁体   中英

php modal login redirecting to another site

The code I have will not redirect to the site in my redirect function. When logging in correctly, the modal login only disappears. This is not a WordPress site. We're using this as a testing page to use for a login page for one of our customers. Here is the code(please excuse the long code):

<?php
session_start();

$userinfo = array(
                'user1'=>'password1',
                'user2'=>'password2'
                );

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location: http://www.espn.com');
}

function redirect() {
    $_SESSION['username'] = '';
    header('Location: http://www.mgoblue.com');
    exit();
}

if(isset($_POST['username'])) {
    if($userinfo[$_POST['username']] == $_POST['password']) {
        $_SESSION['username'] = $_POST['username'];
    }else {
        //Invalid Login
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

</head>
<body>
    <!-- Button to open the modal login form -->
    <button onclick="document.getElementById('id01').style.display='block'">Login</button>

    <!-- The Modal -->
    <div id="id01" class="modal">
        <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>

         <?php if((isset($_SESSION['uname']))): ?>
            <!--<p>You are logged in as <?=$_SESSION['username']?></p>-->
            <!--<p><a href="?logout=1">Click to site</a></p>-->
            <?php redirect(); ?>
        <?php else: ?>

        <!-- Modal Content -->
        <form name="login" class="modal-content animate" action="" method="post">
            <!-- No Avatar!!! -->

            <!-- Login Info -->
            <div class="container">
                <label><b>Username</b></label>
                <input type="text" placeholder="Enter Username" name="username" required><br /><br />

                <label><b>Password</b></label>
                <input type="text" placeholder="Enter Password" name="password" required><br /><br />
                <button type="submit">Login</button>                

            </div>

            <div class="container" style="background-color:#f1f1f1">                
                <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
            </div>
        </form>
        <?php endif; ?>
    </div>       
</body>
</html>

What am I missing in the code, other than the CSS?

In your code, you're using $_SESSION['username'] up top, then checking $_SESSION['uname'] below, which is never set and may be your issue.

This is just a side note really but I'd usually add the redirect code before the location header, as so: header('HTTP/1.1 302 Found'); header('Location: '.$uri);

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