최 호민

최 호민

1604285867

PHP / 회원 관리 / 세션(SESSION) 추가하기, 로그아웃 페이지

login.php

로그인 한 상태인지 로그인 하지 않은 상태인지 파악하기 위해 login-ok.php로 넘어가기 전에 세션을 시작합니다. 그리고 username을 $_SESSION[ ‘username’ ]에 담습니다.


<?php
  $username = $_POST[ 'username' ];
  $password = $_POST[ 'password' ];
  if ( !is_null( $username ) ) {
    $jb_conn = mysqli_connect( 'localhost', 'codingfactory', '1234qwer', 'codingfactory.net_example' );
    $jb_sql = "SELECT password FROM member WHERE username = '" . $username . "';";
    $jb_result = mysqli_query( $jb_conn, $jb_sql );
    while ( $jb_row = mysqli_fetch_array( $jb_result ) ) {
      $encrypted_password = $jb_row[ 'password' ];
    }
    if ( is_null( $encrypted_password ) ) {
      $wu = 1;
    } else {
      if ( password_verify( $password, $encrypted_password ) ) {
        session_start();
        $_SESSION[ 'username' ] = $username;
        header( 'Location: login-ok.php' );
      } else {
        $wp = 1;
      }
    }
  }
?>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>로그인</title>
    <style>
      body { font-family: sans-serif; font-size: 14px; }
      input, button { font-family: inherit; font-size: inherit; }
    </style>
  </head>
  <body>
    <h1>로그인</h1>
    <form action="login.php" method="POST">
      <p><input type="text" name="username" placeholder="사용자이름" required></p>
      <p><input type="password" name="password" placeholder="비밀번호" required></p>
      <p><input type="submit" value="로그인"></p>
      <?php
        if ( $wu == 1 ) {
          echo "<p>사용자이름이 존재하지 않습니다.</p>";
        }
        if ( $wp == 1 ) {
          echo "<p>비밀번호가 틀렸습니다.</p>";
        }
      ?>
    </form>
  </body>
</html>

login-ok.php

  • $_SESSION[ ‘username’ ]의 값을 $session_username에 저장합니다.
  • $session_username에 값이 없으면 login.php로 이동합니다.
  • 값이 있으면 사용자이름과 함께 '로그인 하셨습니다.'를 출력합니다.
  • [로그아웃]을 클릭하면 logout.php로 이동합니다.

<?php
  session_start();
  $session_username = $_SESSION[ 'username' ];
  if ( is_null( $session_username ) ) {
    header( 'Location: login.php' );
  }
?>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>로그인</title>
    <style>
      body { font-family: sans-serif; font-size: 14px; }
      input, button { font-family: inherit; font-size: inherit; }
    </style>
  </head>
  <body>
    <h1><?php echo $session_username; ?>님, 로그인 하셨습니다.</h1>
    <a href="logout.php">로그아웃</a>
  </body>
</html>


This is image title

logout.php

  • 세션을 종료하고 login.php로 이동합니다.
<?php
  session_start();
  session_destroy();
  header( 'Location: login.php' );
?>

#PHP

What is GEEK

Buddha Community

PHP / 회원 관리 / 세션(SESSION) 추가하기, 로그아웃 페이지
최 호민

최 호민

1604285867

PHP / 회원 관리 / 세션(SESSION) 추가하기, 로그아웃 페이지

login.php

로그인 한 상태인지 로그인 하지 않은 상태인지 파악하기 위해 login-ok.php로 넘어가기 전에 세션을 시작합니다. 그리고 username을 $_SESSION[ ‘username’ ]에 담습니다.


<?php
  $username = $_POST[ 'username' ];
  $password = $_POST[ 'password' ];
  if ( !is_null( $username ) ) {
    $jb_conn = mysqli_connect( 'localhost', 'codingfactory', '1234qwer', 'codingfactory.net_example' );
    $jb_sql = "SELECT password FROM member WHERE username = '" . $username . "';";
    $jb_result = mysqli_query( $jb_conn, $jb_sql );
    while ( $jb_row = mysqli_fetch_array( $jb_result ) ) {
      $encrypted_password = $jb_row[ 'password' ];
    }
    if ( is_null( $encrypted_password ) ) {
      $wu = 1;
    } else {
      if ( password_verify( $password, $encrypted_password ) ) {
        session_start();
        $_SESSION[ 'username' ] = $username;
        header( 'Location: login-ok.php' );
      } else {
        $wp = 1;
      }
    }
  }
?>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>로그인</title>
    <style>
      body { font-family: sans-serif; font-size: 14px; }
      input, button { font-family: inherit; font-size: inherit; }
    </style>
  </head>
  <body>
    <h1>로그인</h1>
    <form action="login.php" method="POST">
      <p><input type="text" name="username" placeholder="사용자이름" required></p>
      <p><input type="password" name="password" placeholder="비밀번호" required></p>
      <p><input type="submit" value="로그인"></p>
      <?php
        if ( $wu == 1 ) {
          echo "<p>사용자이름이 존재하지 않습니다.</p>";
        }
        if ( $wp == 1 ) {
          echo "<p>비밀번호가 틀렸습니다.</p>";
        }
      ?>
    </form>
  </body>
</html>

login-ok.php

  • $_SESSION[ ‘username’ ]의 값을 $session_username에 저장합니다.
  • $session_username에 값이 없으면 login.php로 이동합니다.
  • 값이 있으면 사용자이름과 함께 '로그인 하셨습니다.'를 출력합니다.
  • [로그아웃]을 클릭하면 logout.php로 이동합니다.

<?php
  session_start();
  $session_username = $_SESSION[ 'username' ];
  if ( is_null( $session_username ) ) {
    header( 'Location: login.php' );
  }
?>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>로그인</title>
    <style>
      body { font-family: sans-serif; font-size: 14px; }
      input, button { font-family: inherit; font-size: inherit; }
    </style>
  </head>
  <body>
    <h1><?php echo $session_username; ?>님, 로그인 하셨습니다.</h1>
    <a href="logout.php">로그아웃</a>
  </body>
</html>


This is image title

logout.php

  • 세션을 종료하고 login.php로 이동합니다.
<?php
  session_start();
  session_destroy();
  header( 'Location: login.php' );
?>

#PHP

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website?

HourlyDeveloper.io has always been an industry leader for companies and business owners looking to hire PHP web developer. By choosing to Hire PHP Developer from our company, you can always expect the best results. Our PHP services and solutions are always flexible which means that no matter the nature of your project, you can always count on us for getting the best PHP expertise.

Consult with our experts: https://bit.ly/3aEGxPy

#hire php developer #php developer #php development company #php development services #php development #php

Hire PHP Developer - Best PHP Web Frameworks for Web Development

A framework that can drastically cut down the requirement to write original code to develop the web apps as per your requirement is PHP Framework. PHP frameworks offer code libraries for commonly used functions to reduce the development time.

Want to use PHP Web Frameworks for your web applications?

WebClues Infotech offers a service to hire dedicated PHP developers for all of the below-mentioned frameworks

  • Laravel Developer
  • Codeigniter Developer
  • Yii Developer
  • Zend Developer
  • Cake PHP Developer
  • Core PHP Developer

Not sure which framework to use for your PHP web application?

Contact us

Schedule Interview with PHP Developer https://bit.ly/3dsTWf0

Email: sales@webcluesinfotech.com

#hire php developer #hire php web developers #hire php developer in 2021 #hire php developers & dedicated php programmers #hire php developers india #hire and outsource freelance php developers

Mariya James

Mariya James

1613990718

Top PHP web development company| PHP development services India

ValueCoders is a leading PHP app development company that focuses on building robust, secure & scalable web applications for start-ups, enterprises, and entrepreneurs.

We have 16+ years of experience and have delivered custom PHP web development solutions to 2500+ global clients catering industry verticals, including healthcare, adtech, eLearning, data analysis, Fintech, eCommerce, etc

Are you planning to outsource PHP development services? Or want to hire an offshore PHP development team?

#hire php developer #hire a php developer in india #hire dedicated php programmers #hire php coders #php developer in india #php developers for hire

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers?

Hire Dedicated PHP Developer, who can convert your idea to reality, within the stipulated time frame. HourlyDeveloper.io expertise & experience as the top PHP development company put us above our competitors, in many ways. We have some of the top PHP developers in the industry, which can create anything you can imagine, that too, at the most competitive prices.

Consult with our experts:- https://bit.ly/2NpKnB8

#hire dedicated php developer #php developers #php development company #php development services #php development #php developer