如何获取当前登录会话的用户的完整数据?

时间:2017-04-16 19:04:07

标签: php session variables get echo

的init.php

<?php
  session_start();
  require_once "functions/db.php";
  require_once "functions/register.php";
  require_once "functions/login.php";
?>

我的数据库

<?php
$host = "127.0.0.1";
$user = "root";
$password = "";
$db = "wherco";

// create connection
$connect = new mysqli($host, $user, $password, $db);

// check connection
if($connect->connect_error) {
    die("connection failed : " . $connect->connect_error);
} else {
    // echo "Successfully Connected";
}

?>

功能登录

global $connect;

  //mencegah injection
$nama   = mysqli_real_escape_string($connect, $nama);
$pass   = mysqli_real_escape_string($connect, $pass);

$query  = "SELECT password FROM pelanggan WHERE username = '$nama'";
$result = mysqli_query($connect, $query);
//hasil dari fecth assoc adalah array
$hash = mysqli_fetch_assoc($result);


  if ( password_verify($pass, $hash['password']) ){
    return true;
    }else{
    return false;
  }

}

//test name in the database
function login_cek_nama($nama){
  global $connect;

  $nama = mysqli_real_escape_string($connect, $nama);

  $query = "SELECT * FROM pelanggan WHERE username = '$nama'";


  if ( $result = mysqli_query($connect, $query) ){
    if(mysqli_num_rows($result) != 0) return true;
    else return false ;
  }
}
 ?>

登录页面

require_once "core/init.php";

if(isset($_SESSION['user']) ){
          $_SESSION['msg'] = 'please logout ';
        header('Location: index.php');
}

 $error='';

if( isset($_POST['submit']) ){
  $nama   = $_POST['username'];
  $pass   = $_POST['password'];

  if(!empty(trim($nama)) && !empty(trim($pass)) ){

if(login_cek_nama($nama) ){
    if(cek_data($nama, $pass)) {
      $_SESSION ['user'] = $nama;//  important
      header('Location:order.php');
    }else{
      $error= 'data ada yang salah';
    }

}else{
  $error= 'name has not been registered';
}


  }else{
    $error= 'can not be empty';
  }
}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title></title>

  <!-- Latest compiled and minified css -->
     <link rel="stylesheet" href="view/css/bootstrap.min.css">
  <!-- optional theme-->
     <link rel="stylesheet" href="view/css/bootstrap-theme.min.css">
  <!--my custom css-->
     <link rel="stylesheet" href="view/css/style.css">
  <!--font-awesome-->
     <link rel="stylesheet" href="view/font-awesome/css/font-awesome.min.css">


<!---------------------------------->

<style>

body,
html{

  font-family: arial monospace;
  background-color :#eee;
}

.container{
  display: flex;
  align-items: center;
  justify-content: center;
}

.page{

height: 300px;
width: 300px;
background: #fff;
margin-top: 70px;
margin-bottom: 70px;
}

.content{
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.logo-register h2{
font-family:Chaparral Pro Light;
font-weight: bold;
font-style:none;
}

.logo-register h5{
  font-family: calibri;
  color:#808080;
  border-bottom:1px solid #eee;
  padding-bottom: 5px;

}

.form-group .form-control{
  border-radius: 0;
  border:1px solid #eee;
  padding: 5px;
}

.bawah-submit h5{  font-family: calibri;
                                color:#808080;}

.punya-akun h5{font-family: calibri;
                    color:#808080;}

#error{color: rgb(212, 19, 65);
}

</style>
</head>

<body>

              <div class="container">

                          <div class="page">
                                <div class="content">
                                    <div class="row">
                                        <div class="logo-register">
                                                <h2><a href="index.php">Wherco</a></h2>
                                              <h5>login for order</h5>
                                        </div>

                                        <?php if($error != ' '){ ?>
                                            <div id="error">
                                                <?php echo $error; ?>
                                            </div> <br>
                                          <?php } ?>

                                  <form class="form-horizontal" action="login.php" method="post">
                                        <div class="form-group">
                                             <input type="text" class="form-control" style="text-transform:lowercase;"  
                                              name ="username" placeholder="Nama lengkap" Required autofocus>
                                          </div>

                                            <div class="form-group">
                                                 <input type="password" style="text-transform:lowercase;"  class="form-
                                                 cotrol" name="password" id="exampleInputEmail1" placeholder="Kata 
                                                 sandi" Required>
                                            </div>

                                            <div class="form-group">
                                              <input type="submit" name="submit" 
                                               class="btn btn-info btn-block" value="Login">
                                            </div>


                                            <div class="punya-akun">
                                       <h5>not have an account? <a href="register.php">Daftar</a> </h5>
                                          </div>

                                          <!--<div class="">
                                            <h5> have account? <a href="#">login</a> </h5>
                                          </div>-->
                                </div>
                          </div>
                      </div>
                    </div

功能寄存器

<?php

function register_user($nama, $email, $alamat, $telp, $pass){
global $connect;

//mencegah sql injection
$nama   = mysqli_real_escape_string($connect, $nama);
$email  = mysqli_real_escape_string($connect, $email);
$alamat = mysqli_real_escape_string($connect, $alamat);
$telp   = mysqli_real_escape_string($connect, $telp);
$pass   = mysqli_real_escape_string($connect, $pass);


$pass = password_hash($pass, PASSWORD_DEFAULT);


  $query = "INSERT INTO pelanggan (username, email, alamat, telp, password) VALUES ('$nama', '$email', '$alamat', '$telp', '$pass')";

  if( mysqli_query($connect, $query) ){

        return true;
      }else{
        return false;
  }
}

//check names twins
function register_cek_nama($nama){
global $connect;

$nama   = mysqli_real_escape_string($connect, $nama);


$query  = "SELECT * FROM pelanggan WHERE username ='$nama'";

if( $result = mysqli_query($connect, $query) ){
  if(mysqli_num_rows($result) == 0) return true;
  else return false;
  }
}

?>

注册页面

<?php require_once "core/init.php";

$error='';

if( isset($_POST['submit']) ){
  $nama   = $_POST['username'];
  $email  = $_POST['email'];
  $alamat = $_POST['alamat'];
  $telp   = $_POST['telp'];
  $pass   = $_POST['password'];


if(!empty(trim($nama)) && !empty(trim($email)) && !empty(trim($alamat)) && !empty(trim($telp)) && !empty(trim($pass)) ){



  if(register_cek_nama($nama, $email) ){
    //memasukan database
          if(register_user($nama, $email, $alamat, $telp, $pass)) {
            $error= 'successfully register';
          }else{
            $error= 'fail register';}
      }else{
      $error= ' existing name may not register ';}
  }else{
      $error= 'form can not be empty';
  }
}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title></title>

  <!-- Latest compiled and minified css -->
     <link rel="stylesheet" href="view/css/bootstrap.min.css">
  <!-- optional theme-->
     <link rel="stylesheet" href="view/css/bootstrap-theme.min.css">
  <!--my custom css-->
     <link rel="stylesheet" href="view/css/style.css">
  <!--font-awesome-->
     <link rel="stylesheet" href="view/font-awesome/css/font-awesome.min.css">


<!---------------------------------->

<style>

body{

  font-family: arial monospace;
  background-color :#eee;
}

.container{

  display: flex;
  align-items: center;
  justify-content: center;}

.page{

padding:5px;
margin-top: 40px;
margin-bottom: 40px;
height: 510px;
width: 400px;
background: #fff;
}

.content{
  height: 100%;
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.logo-register h2{
  font-family:Chaparral Pro Light;
font-weight: bold;
}

.logo-register h5{
  font-family: calibri;
  color:#808080;
  border-bottom:1px solid #eee;
  padding-bottom: 5px;

}

.form-group .form-control{
  border-radius: 0;
  border:1px solid #eee;
  padding: 5px;
}

.bawah-submit h5{  font-family: calibri;
                                color:#808080;}

.punya-akun h5{font-family: calibri;
                    color:#808080;}

  #error{color: rgb(212, 19, 65);
                    }

</style>
</head>

<body>
              <div class="container">

                          <div class="page">
                                <div class="content">
                                    <div class="row">
                                        <div class="logo-register">
                                                <h2><a href="index.php">Wherco</a></h2>
                                              <h5>Buat akun untuk melakukan order</h5>
                                        </div>


                                              <?php
                                                  if(isset($_SESSION['msg'])) { ?>
                                                <div id="error">
                                                    <?php echo $_SESSION['msg'];
                                                    unset($_SESSION['msg']); ?>
                                                </div> <br>
                                              <?php }  ?>

                                        <?php if($error != ' '){ ?>
                                            <div id="error">
                                                <?php echo $error; ?>
                                            </div>
                                          <?php } ?>


                                  <form class="form-horizontal" action="register.php" method="post">
                                        <div class="form-group">
                                             <input type="text" class="form-control" style="text-transform:lowercase;"  name="username" placeholder="Nama lengkap" Required autofocus>
                                          </div>

                                          <div class="form-group">
                                               <input type="email" class="form-control"  name="email" placeholder="Alamat email" Required>
                                          </div>

                                          <div class="form-group">
                                              <textarea class="form-control" rows="3"  name="alamat" placeholder="Alamat tinggal" Required autofocus ></textarea>
                                            </div>

                                            <div class="form-group">
                                                 <input type="number" class="form-control"  name="telp" placeholder="Nomer yang bisa di hubungi" Required>
                                            </div>

                                            <div class="form-group">
                                                 <input type="password" style="text-transform:lowercase;"  class="form-control" name="password"   id="exampleInputEmail1" placeholder="Kata sandi untuk akun order" Required>
                                            </div>

                                            <div class="form-group">
                                              <input type="submit" name="submit" class="btn btn-info btn-block" value="Daftar">
                                            </div>


                                            <div class="bawah-submit">
                                                    <h5> Dengan mendaftar anda menyetujui Ketentuan<br>
                                                    dan Kebijakan Privasi kami </h5>
                                          </div>

                                          <div class="punya-akun">
                                            <h5> Punya akun? <a href="login.php">Masuk</a> </h5>
                                          </div>
                                </div>
                          </div>
                      </div>
                    </div

订购页面(我的问题来自)

<?php
require_once "core/init.php";

if( !isset($_SESSION['user']) ){

        $_SESSION['msg'] = 'must have an account to order';
        header('Location: register.php');
}
 ?>

 <?php
 require_once "view/header.php";
  ?>

<style>

body,
html{
  font-family: arial monospace;
  padding:0;

}

.bagian-judul-form-1{

}

.bagian-judul-form-2{
  color:#808080;
}

.bagian-kiri a{
  color:blue;
}

.bagian-kiri{
  color:#808080;
  margin-bottom: 20px;
  padding : 2px;
    }

.form-group .form-control{
  border-radius:0;
}

.form-control{
 border-radius:0;
}


</style>

<div class="tengah">

<div class="container">

    <div class="row">


      <div class="col-md-3 col-sm-12 col-xs-12">
          <div class="bagian-kiri">
            <h4><p class="">Kurang yakin? apakah mau lihat <a href="portofolio.php">portofolio</a> kami dahulu? atau bingung bagaimana <a href="order">sistem order </a> kami berjalan? kami siap membantu :) </p></h4>
          </div>
      </div>



    <div class="col-md-offset-3 col-md-6">
          <div class="bagian-judul-form-1">
            <h3>Form untuk melakukan order</h3>
          </div>

                <div class="bagian-judul-form-2">
                <h5><p class="text-justify">Mohon form ini di isi dengan jelas dan benar,
                                                                untuk memudahkan proses verivikasi order
                                                                untuk jasa desaign anda.
                                                                Terima Kasih.</p></h5>
                </div>
    </div>

    <div class="row">
        <div class="col-md-offset-6 col-md-5 col-md-offset-6">

                <fieldset disabled>
                  <div class="form-group">
                       <input type="nama" class="form-control"  id="disabledTextInput" value="<?php
                       echo $_SESSION['user'];?>"  placeholder="Nama">
                  </div>

                  <div class="form-group">
                       <input type="email" class="form-control"  id="disabledTextInput" value="<?php
                       echo $_SESSION['email'];?>"  placeholder="Email">   <!----- saat melakukan pemanggilan email , email tidak di kenali ---->
                  </div>
                </fieldset>

                  <div class="form-group">
                  <select class="form-control">
                          <option>none package</option>
                          <option>Broze</option>
                          <option>Silver</option>
                          <option>Gold</option>
                    </select>
                  </div>

                  <div class="form-group">
                      <textarea class="form-control" rows="3" placeholder="Deskripsikan latar belakang. contoh: penjualan kopi bertema minamal dengan tempat di tengah kota. nama toko -kopi rakyat-  " Required autofocus></textarea>
                  </div>

                    <div class="form-group">
                      <input type="submit" name="submit" class="btn btn-info btn-block" value="Order">
                    </div>

        </div>

</div>
</div>
</div>
</div>

<?php
require_once "view/footer.php"
?>

我的桌子名是pelanggan, 我的结构表 - &gt; id_pelanggan,用户名,emai,alamat,telp,密码

我的问题是如何显示数据$ _SESSION ['alamat'],$ _SESSION ['email'],另一个正在记录,如何设置,没有人可以帮我这个码?我是使用功能的新手。需要帮忙。我知道我只是设置用户会话(login.php),但我不知道我是怎么做的代码?我只想以order.php上的数据形式显示它。谢谢

0 个答案:

没有答案