PHP does not create a successful login session

Asked 2 years ago, Updated 2 years ago, 42 views

I am currently making a simple bulletin board with PHP.

Therefore, when I press the edit button from the post list page, I would like to add a function that allows me to jump to the edit screen if I am logged in and return to the post list screen if I am not logged in.

The list of posts is admin.php and the edit screen is edit.php.

However, even if you log in and press the edit button on admin.php, it will always be returned from edit.php to admin.php.

I want you to stay at edit.php when you are logged in well.

Below is the code for edit.php.

<?php
var_dump($_SESSION['admin_login']);
exit();
$link=mysqli_connect("localhost", "root", "root", "keijiban");

date_default_timezone_set('Asia/Tokyo');

if(empty($_SESSION['admin_login'])||$_SESSION['admin_login']!==true){

    // Redirect to Login Page
    header("Location: ./admin.php");
}

if(!empty($_GET['comment_id'])&empty($_POST['comment_id']){

    $comment_id=(int)htmlspecialchars($_GET['comment_id',ENT_QUOTES);

    // Connect to Database
    $mysqli = new mysqli("localhost", "root", "root", "keijiban");

    // Verifying Connection Errors
    if($mysqli->connect_errno){
        $error_message[] = 'Database connection failed. Error number '.$mysqli->connect_errno.': '.$mysqli->connect_error;
    } else{

        // Loading Data
        $sql="SELECT* FROM comment WHERE id=$comment_id";
        $res=$mysqli->query($sql);

        if($res){
            $arrayData=$res->fetch_assoc();
        } else{

            // Return to list if no data is read
            header("Location: ./admin.php");
        }

        $mysqli->close();
    }

} elseif(!empty($_POST['comment_id']){

}

?>

<!DOCTYPE html>
<html lang="ja">
<head>
<metacharset="utf-8">
<title>Terry Bulletin Board Management Page (Edit Posts) </title>
<style>

.btn_cancel{
    display:inline-block;
    margin-right —10px;
    padding —10px20px;
    color:#555;
    font-size: 86%;
    border-radius:5px;
    border —1px solid#999;
}
.btn_cancel:hover{
    color:#999;
    border-color:#999;
    text-decoration: none;
}

</style>
</head>
<body>
<h1>Terry bulletin board management page (editing posts)</h1>
<?php if(!empty($error_message): ?>
    <ul class="error_message">
        <?php foreach($error_message as$value): ?>
            <li> ·<?phpecho$value;?></li>
        <?php endforeach;?>
    </ul>
<?php endif;?>
<div class="container">

          <form method="post">
        <div class="form-group">
          <label for="form-mail">Username</label>
          <input type="username" class="form-control" name="username" placeholder="tree" value="<?php if(!empty($arrayData['username'])) {echo$arrayData['username'];}?>
        </div>
        <div class="form-group">
          <label for="exampleInputComment">Comments</label>
          <input type="comment" class="form-control" name="comment" placeholder="Post freely!" value="<?php if(!empty($arrayData['comment'])) {echo$arrayData['comment'];}?>">
        </div>
        <a class="btn_cancel" href="admin.php">Cancel</a>
        <button type="submit" class="btn btn-primary">Update</button>
        <input type="hidden" name="comment_id"value="<?phpecho$arrayData['id'];?>">
      </form>

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

php mysql

2022-09-30 18:52

1 Answers

I'm a beginner at php.
If it hasn't been resolved yet, just for your information.

Looking at the source, there are two conditions for redirecting to ./admin.php.

One is when the SESSION value is not included and the second is when SQL execution fails.

I think the following are possible reasons why the value of SESSION is not included.

  • Check the key name of the SESSION for typographical errors.
  • Are you restricted from using SESSION in your browser or server settings?Check the config, as it may be (completely guessed).
  • If you use SESSION, you'll have to run session_start() in the script first, so try adding a line of session_start() at the beginning of the script.

I think the reasons for SQL execution failures are as follows:

  • Unable to connect to DB.Verify that the server name, DB name, and table name are correct.
  • The SQL statement may be strange.I think you are generating SQL statements in the script, but I will check the generated SQL statements by debugging them as print or echo.

The above may be the cause, so please check it out.


2022-09-30 18:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.