简体   繁体   中英

PHP- Redirect to home page after submit login form doesn't work on server but works fine on localhost

when I submit username and password form, its not redirect to home.php even both are correct, I tested my site on localhost working fine but when upload to server doesn't work. I have to refresh page after submit form even I got message says Invalid username or password!

Login.php

    <?php
require_once 'config.php';
$username = $_POST['username'];
$password = $_POST['password'];
$q_login = $conn->query("SELECT * FROM `admin` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());
$f_login = $q_login->fetch_array();
$v_login = $q_login->num_rows;
if($v_login > 0){
echo 'success';
session_start();
$_SESSION['admin_id'] = $f_login['admin_id'];
}

index.php(Form)

    <?php
session_start();
if(ISSET($_SESSION['admin_id'])){
header('location: home.php');
}
?>
<html lang = "eng">
<head>

    <meta charset = "utf-8" />
    <meta name = "viewport" content = "width=device-width, initial-scale=1" />
    <link rel = "stylesheet" href = "css/bootstrap.css" />

</head>
<body>
    <nav class = "navbar navbar-inverse navbar-fixed-top">
        <div class = "container-fluid">
            <div class = "navbar-header">
                <p class = "navbar-text pull-right">Atolito Board System</p>
            </div>
        </div>
    </nav>
    <div class = "container" style = "margin-top:120px;">

                        <h4>Admin Login</h4>
                    </div>
                    <div class = "panel-body">
                        <form enctype = "multipart/form-data">
                            <div id = "username_warning" class = "form-group">
                                <label class = "control-label">Username:</label>
                                <input type = "text" id = "username" class = "form-control" />
                            </div>
                            <div id = "password_warning" class = "form-group">
                                <label class = "control-label">Password:</label>
                                <input type = "password" maxlength = "12" id = "password" class = "form-control" />
                            </div>
                            <div id = "result"></div>
                            <br />
                            <button type = "button" class = "btn btn-primary btn-block" id = "login_admin"><span class = "glyphicon glyphicon-save"></span> Login</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>
<script src = "js/jquery.js"></script>
<script src = "js/bootstrap.js"></script>
<script src = "js/login.js"></script>
</html>

validator.php

    <?php
session_start();
if(!ISSET($_SESSION['admin_id'])){
header('location: home.php');
}
  • Check your config.php it may includes localhost configs.
  • Use isset instead of ISSET
  • You may need to add <form action="login.php" method="POST"> in your html code.

before

session_start(); 

add

session_name('someYourName');

So it will look like:

<?php
session_name('someYourName');
session_start(); 

Use same name in all files you want use same session. In your way you have different sessions for each php file.

On login.php

echo 'success';
session_start();

So if you want with "echo" you should do it like this:

session_name('someYourName');
session_start();
echo 'success';

You can't send any content before session start - best practice is to start session on beginning of page.

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