用户登录PHP后重定向到同一页面 [英] Redirect to the same page after user login PHP

查看:71
本文介绍了用户登录PHP后重定向到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户返回到用户上次访问的页面.例如,用户单击主页上的登录按钮,它将转到登录表单页面.单击登录后,它将重定向回主页.我现在使用的编码可以很好地用于登录表单页面到主页(即Facebook登录)的操作.但是,当我尝试做一个我之前说过的例子时,它是行不通的.出现此页面无法正常工作"错误.我发现问题出在session.php上,我尝试进行一些更改,但仍无法按我想要的方式工作.有人可以在PHP编码方面为我提供帮助吗?我仍然是使用这种PHP编码的新手.

I want to make the user go back to the the page that the user last visit. For example, user click at the login button at the homepage and it will go to login form page. After click login, it will redirect back to the homepage. The coding that I use right now work fine for login form page to homepage(i.e Facebook login). However, when I try to make like example that I said before, it's not working. It's give "This page isn’t working" error. I find that the problem is at the session.php and I try to change a bit but still not working like I want. Can someone help me at the PHP coding. I still new using this PHP coding.

下面是我的编码.

index.php(HOMEPAGE)

index.php(HOMEPAGE)

<?php
    include('session.php');
    ?>

      <div class="header-right">
        <?php if (isset($_SESSION['login_user'])) { ?>
            <a class="active" >
                <img src="image/user.png" width="22" height="22">
                <?= $login_session ?>
                <img src="image/drop.png" width="20" height="20" >
            </a>
            <div class="box dropdown">
                <ul>
                    <a href="#">edit</a>
                    <a href="#">delete</a></li>
                    <a id="logout" href="logout.php">Log Out</a>
                </ul>
            </div>
        <?php } else { ?>
            <a class="active" href="login.php"><img src="image/lock.png" width="20" height="20"> Login</a>
        <?php } ?>
    </div>
    </div>

session.php

session.php

 <?php
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $connection = mysql_connect("localhost", "root", "");
    // Selecting Database
    $db = mysql_select_db("company", $connection);
    session_start();// Starting Session
    // Storing Session
    $user_check=$_SESSION['login_user'];
    // SQL Query To Fetch Complete Information Of User
    $ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
    $row = mysql_fetch_assoc($ses_sql);
    $login_session =$row['username'];
    if(!isset($login_session)){
    mysql_close($connection); // Closing Connection
    header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Same Page
    }
    ?>

login.php(登录表单页面)

login.php(Login Form Page)

 <?php
    include('loginsuccess.php'); // Includes Login Script

    if(isset($_SESSION['login_user'])){
    header('Location: '.$_SERVER['HTTP_REFERER']);
    }
    ?>

    <form class="form" action="" method="post">
                  <input type="text" placeholder="Username" required name="username">
                  <input id="myInput" type="password" placeholder="Password" required name="password">
                  <p id="text">Caps lock is ON</p> 

                  <button type="submit" id="login-button" name="submit" value" Login">Login</button>
                  <span><?php echo $error; ?></span>
                </form>

loginsuccess.php

loginsuccess.php

  <?php
    session_start(); // Starting Session
    $error=''; // Variable To Store Error Message
    if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
    $error = "Username or Password is invalid";
    }
    else
    {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $connection = mysql_connect("localhost", "root", "");
    // To protect MySQL injection for Security purpose
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    // Selecting Database
    $db = mysql_select_db("company", $connection);
    // SQL query to fetch information of registerd users and finds user match.
    $query = mysql_query("select * from login where password='$password' AND username='$username'", $connection);
    $rows = mysql_num_rows($query);
    if ($rows == 1) {
    $_SESSION['login_user']=$username; // Initializing Session
    header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Other Page
    } else {
    $error = "Username or Password is invalid";
    }
    mysql_close($connection); // Closing Connection
    }
    }
    ?>

推荐答案

按如下所示修改session.php:

Modify your session.php as follows:

header('Location: '.$_SERVER['HTTP_REFERER']);

<?php
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $connection = mysql_connect("localhost", "root", "");
    // Selecting Database
    $db = mysql_select_db("company", $connection);
    session_start();// Starting Session
    // Storing Session
    $user_check=$_SESSION['login_user'];
    // SQL Query To Fetch Complete Information Of User
    $ses_sql=mysql_query("select username from login where username='$user_check'", $connection);
    $row = mysql_fetch_assoc($ses_sql);
    $login_session =$row['username'];
    if(!isset($login_session)){
    mysql_close($connection); // Closing Connection
    header('Location: '.$_SERVER['HTTP_REFERER']); // Redirecting To Same Page
    exit(); 
    }
    ?>

这篇关于用户登录PHP后重定向到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆