如何在不再登录我的同一页面上停留在同一页面上?

时间:2016-07-16 18:53:47

标签: php

当我编写localhost / store时,如何保持同一页面 1.-当我进入我的系统时,我有这个url localhost / store / view / home.php,但我写了localhost / store然后它再次返回我的登录页面。当我编写localhost / store时,如何将页面返回到home.php或任何页面。 控制器

<?php

require_once('../model/user_model.php');

$boton = $_POST['boton'];

switch ($boton) {
    case 'logout':
        session_start();

        if (isset($_SESSION['session_user_id'])){
            $user = new user();
            $user->updateCheckPoint($_SESSION['session_user_id']);
        }
        session_destroy();
        break;
    case 'login':
        if(isset($_POST['username']) && $_POST['username'] != '' && isset($_POST['password']) && $_POST['password'] != ''){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $username = stripslashes($username);
            $username = mysql_real_escape_string($username);
            $password = stripslashes($password);
            $password = mysql_real_escape_string($password);
            $ins = new user();
            $array = $ins->identification($username, $password);
            if($array[0] == 0){
                echo 0;
            }else{
                session_start();
                $_SESSION['login'] = 'YES'; 
                $_SESSION['full_name'] = $array[1] . ' ' . $array[2];
                $_SESSION['session_type_user'] = $array[5];
                $_SESSION['job_position'] = $array[6];
            }
        }else{
            echo 'something went wrong';
        }
     break;
   }
?>

模型

<?php
    if (session_id() === ''){
        session_start();
    }

    class user{
    private $conexion;

    public function __construct(){
        require_once('conexion.php');
        $this->conexion = new conexion();
        $this->conexion->conectar();
    }

    function identification($username,$password){
        $pass = md5(sha1($password));
        $sql="SELECT iduser,name,last_name,username,password,typeuser_idtypeuser,type from user INNER JOIN typeuser ON user.typeuser_idtypeuser = typeuser.idtypeuser WHERE username = '$username' AND password = '$pass'";
        $record = $this->conexion->conexion->query($sql);
        if ($record->num_rows > 0){
            $row = $record->fetch_array();
            session_regenerate_id();
            $_SESSION['session_user_id'] = $row['iduser'];
            $_SESSION['session_username'] = $row['username'];
            $_SESSION['session_type_user'] = $row['typeuser_idtypeuser'];
            echo $_SESSION['session_type_user'];
            session_write_close();
        }else{
            $row[0] = 0;
        }
        return $row;
        $this->conexion->cerrar();
    }

    public function updateCheckPoint($id_user){
        $id_user = intval($id_user);
        $t = time();
        $sql = "UPDATE user SET checkpoint = {$t} WHERE iduser = {$id_user};";
        $this->conexion->conexion->query($sql);
        $this->conexion->cerrar();
    }
  }
?>

的index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>punto</title>

    <link rel="stylesheet" href="../assests/css/bootstrap.min.css">
    <link rel="stylesheet" href="../assests/css/main.css">
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="navbar-header">
            <a href="#" class="navbar-brand">Store</a>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-4">
                <div class="panel panel-default login">
                    <div class="panel-heading">Sign In</div>
                    <div class="panel-body">       
                        <form data-toggle="validator" role="form" method="POST" onsubmit="return login();">
                            <div class="form-group">
                                <label for="username">Username</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                                    <input type="text" class="form-control" name="username" id="username" placeholder="Username" required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-star"></span></span>
                                    <input type="password" class="form-control" id="password" placeholder="Password" required>
                                </div>
                            </div>     
                             <div class="alert alert-danger text-center" style="display:none;" id="error">
                                <p>Invalid username or password !!</p>
                            </div>    
                            <button type="submit" class="btn btn-lg btn-success btn-block" onsubmit="login();"><span class="glyphicon glyphicon-lock frm-general"></span>Login</button>   
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../assests/js/jquery-1.11.2.js"></script>
    <script src="../assests/js/bootstrap.min.js"></script>
    <script src="../assests/js/validator.min.js"></script>
    <script>
      function login(){
                var username = $('#username').val();
                var password = $('#password').val();
                $.ajax({
                    url:'../controller/login_controller.php',
                    type:'POST',
                    data:'username='+username+'&password='+password+"&boton=login"
                }).done(function(resp){
                    console.log(resp);
                    if(resp === '0'){
                        $('#error').show().delay(2000).fadeOut();
                         reset = $('#username,#password').val('');
                    }else if(resp === '1'){
                        location.href='../view/home.php';
                    }else{  
                        location.href='../view/home.php';
                    }
                });
                return false;
            }
    </script>
</body>
</html>

home.php

    <?php
session_start();
if (isset($_SESSION['login']) && $_SESSION['login']=='YES'){?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Store</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../assests/css/bootstrap.min.css">
    <link rel="stylesheet" href="../assests/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../assests/ionicons-2.0.1/css/ionicons.min.css">
    <link rel="stylesheet" href="../assests/dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="../assests/dist/css/skins/_all-skins.min.css">
  </head>
  <body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">

      <header class="main-header">
        <!-- Logo -->
        <a href="index2.php" class="logo">
          <!-- mini logo for sidebar mini 50x50 pixels -->
          <span class="logo-mini"><b>A</b>LT</span>
          <!-- logo for regular state and mobile devices -->
          <span class="logo-lg"><b>Store</b>LTE</span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top" role="navigation">
          <!-- Sidebar toggle button-->
          <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
            <span class="sr-only">Toggle navigation</span>
          </a>
          <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">
              <!-- Notifications: style can be found in dropdown.less -->

              <li id="notifications" class="dropdown">
                    <a id="notifications-button" class="dropdown-toggle" data-toggle="dropdown" href="#">
                        <i class="fa fa-bell fa-fw"></i><span id="notifications-badge" class="label label-danger" style="display: none">0</span>
                    </a>

                    <ul id="notifications-list" class="dropdown-menu dropdown-tasks">
                        <li>
                            <a class="text-center" href="#">
                                <strong>See All Notifications</strong>
                                <i class="fa fa-angle-right"></i>
                            </a>
                        </li>
                    </ul>
                </li>
              <!-- User Account: style can be found in dropdown.less -->
              <li class="dropdown user user-menu">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <img src="../assests/img/user2-160x160.jpg" class="user-image" alt="User Image">
                  <span class="hidden-xs"><?php echo $_SESSION['full_name']; ?></span>
                </a>
                <ul class="dropdown-menu">
                  <!-- User image -->
                  <li class="user-header">
                    <img src="../assests/img/user2-160x160.jpg" class="img-circle" alt="User Image">
                    <p>
                      <?php echo $_SESSION['full_name']; ?> 
                      <small><?php echo $_SESSION['job_position'];?></small>
                    </p>
                  </li>
                  <!-- Menu Body -->
                  <li class="user-body">
                    <div class="col-xs-4 text-center">
                      <a href="#"></a>
                    </div>
                    <div class="col-xs-4 text-center">
                      <a href="#"></a>
                    </div>
                  </li>
                  <!-- Menu Footer-->
                  <li class="user-footer">
                    <div class="pull-right">
                      <a href="javascript:void(0)" class="btn btn-default btn-flat" onclick="cerrar()">Sign out</a>
                    </div>
                  </li>
                </ul>
              </li>
            </ul>
          </div>
        </nav>
      </header>
      <!-- Left side column. contains the logo and sidebar -->
      <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
          <!-- sidebar menu: : style can be found in sidebar.less -->
          <ul class="sidebar-menu">
            <li class="header">MENÚ</li>
            <li class="active">
              <a href="home.php">
                <i class="fa fa-dashboard fa-fw"></i>
                <span>Home</span>
                <span class="label label-primary pull-right">4</span>
              </a>
            </li>
             <li class="treeview">
              <a href="#">
                <i class="fa fa-th"></i> <span>Inventory</span>
                <i class="fa fa-angle-left pull-right"></i>
              </a>
              <ul class="treeview-menu">
                <li><a href="product.php"><i class="fa fa-circle-o text-aqua"></i> Product</a></li>
              </ul>
            </li>
            <li class="treeview">
              <a href="#">
                <i class="fa fa-th"></i> <span>Purchase</span>
                <i class="fa fa-angle-left pull-right"></i>
              </a>
              <ul class="treeview-menu">
                 <li><a href="provider.php"><i class="fa fa-circle-o text-aqua"></i> Provider</a></li>
              </ul>
            </li>
            <li class="treeview">
              <a href="sale.php">
                <i class="fa fa-shopping-cart"></i>
                <span>Sale</span>
              </a>
            </li>
            <li class="treeview">
              <a href="#">
                <i class="fa fa-pie-chart"></i> <span>Charts</span>
                <i class="fa fa-angle-left pull-right"></i>
              </a>
              <ul class="treeview-menu">
                <li><a href="sales_chart.php"><i class="fa fa-circle-o text-red"></i> Best selling product</a></li>
              </ul>
            </li>
            <li class="treeview">
              <a href="#">
                <i class="glyphicon glyphicon-user"></i> <span>User</span>
                <i class="fa fa-angle-left pull-right"></i>
              </a>
              <ul class="treeview-menu">
                <li><a href="pages/examples/invoice.html"><i class="fa fa-circle-o"></i> Invoice</a></li>
                <li><a href="pages/examples/profile.html"><i class="fa fa-circle-o"></i> Profile</a></li>
                <li><a href="pages/examples/login.html"><i class="fa fa-circle-o"></i> Login</a></li>
                <li><a href="pages/examples/register.html"><i class="fa fa-circle-o"></i> Register</a></li>
                <li><a href="pages/examples/lockscreen.html"><i class="fa fa-circle-o"></i> Lockscreen</a></li>
                <li><a href="pages/examples/404.html"><i class="fa fa-circle-o"></i> 404 Error</a></li>
                <li><a href="pages/examples/500.html"><i class="fa fa-circle-o"></i> 500 Error</a></li>
                <li><a href="pages/examples/blank.html"><i class="fa fa-circle-o"></i> Blank Page</a></li>
              </ul>
            </li>
          </ul>
        </section>
        <!-- /.sidebar -->
      </aside>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Sale
          </h1>
        </section>
        <!-- Main content -->
        <section class="content">
          <!-- Small boxes (Stat box) -->
          <div class="row">
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
              <div class="info-box">
                <span class="info-box-icon bg-red"><i class="fa fa-google-plus"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text">Likes</span>
                  <span class="info-box-number">41,410</span>
                </div><!-- /.info-box-content -->
              </div><!-- /.info-box -->
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
              <div class="info-box">
                <span class="info-box-icon bg-green"><i class="ion ion-ios-cart-outline"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text">Sales</span>
                  <span class="info-box-number">760</span>
                </div><!-- /.info-box-content -->
              </div><!-- /.info-box -->
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
             <div class="info-box">
                <span class="info-box-icon bg-yellow"><i class="ion ion-ios-people-outline"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text">New Members</span>
                  <span class="info-box-number">2,000</span>
                </div><!-- /.info-box-content -->
              </div><!-- /.info-box -->
            </div><!-- ./col -->
            <div class="col-lg-3 col-xs-6">
              <!-- small box -->
             <div class="info-box">
                <span class="info-box-icon bg-aqua"><i class="ion ion-ios-gear-outline"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text">CPU Traffic</span>
                  <span class="info-box-number">90<small>%</small></span>
                </div><!-- /.info-box-content -->
              </div><!-- /.info-box -->
            </div><!-- ./col -->
          </div><!-- /.row -->
          <!-- Main row -->
          <div class="row">
            <!-- Left col -->
            <section class="col-lg-12 connectedSortable">
                <div id="salesChart"></div>
            </section>
        </section>
      </div>
    </div>
    <script src="../assests/js/jQuery-2.1.4.min.js"></script>
    <script src="../assests/js/bootstrap.min.js"></script>
    <script src="../assests/js/jquery.slimscroll.min.js"></script>
    <script src="../assests/dist/js/app.min.js"></script>
    <script src="../assests/noty/packaged/jquery.noty.packaged.min.js"></script>
    <script src="../assests/js/moment.min.js"></script>
    <script src="../assests/js/notifications.js"></script>
    <script src="../assests/js/highcharts.js"></script>
    <script src="../assests/js/salesChart.js"></script>
    <script>
        function cerrar(){
            $.ajax({
                url:'../controller/login_controller.php',
                type:'POST',
                data:"boton=logout"
            }).done(function(resp){
                location.href = '../view/'
            });
        }
    </script>
</body>
</html>
<?php 
  }
  else
    header("location: ./");
 ?>  

1 个答案:

答案 0 :(得分:0)

将此代码放在index.php

的顶部
<?php
    session_start();

    if(isset($_SESSION['login']) && $_SESSION['login'] == 'YES'){
        die(header("Location: home.php"));
    }
?>