PHPでショッピングカートを!【11.登録会員ログイン】

PHP

前回まで、顧客情報入力画面から会員登録するか
しないかを選択する画面を作りました。

今回は、会員登録を完了させ、登録会員ログインを
作成していきます。

顧客情報入力完了

顧客情報入力画面から会員登録をすると
同時に注文確定にもなります。

ファイル名:inputinfo_end.php

<記述例>

<?php
session_start();
session_regenerate_id(true);
?>
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>顧客情報入力完了</title>
    <style type="text/css">
<!--
.txtbox{display: inline-block;margin: 10px;}
h4{background-color:mediumaquamarine;padding: 3px 10px;}
table {margin: 10px auto;padding: 10px;border-collapse: collapse;border: 2px solid seagreen;}
table tr:nth-child(even) {background: mintcream;}
table tr:nth-child(odd) {background: honeydew;}
table tr th,td {border: 1px solid seagreen;padding: 5px 10px; text-align: left;}
table tr th {background-color:mediumaquamarine;}
table .right{text-align: right;}
.btnbox{text-align: center;}
.btn1{color: honeydew;font-size: 16px;background: gray;border-radius: 30px 0 0 30px;padding: 10px 24px;margin: 5px 10px;}
-->
    </style>
  </head>
  <body>
    <main>
      <section class="txtbox">

<?php

require_once('../com_func.php');

$post=sanitize($_POST);

$cname=$post['cname'];
$email=$post['email'];
$postal=$post['postal'];
$address=$post['address'];
$tel=$post['tel'];
$note=$post['note'];
$mem=$post['mem'];
$lpass=$post['lpass'];

print $cname.' 様<br>';
print 'ご注文ありがとうございました。<br>';
print $email.' 宛に自動返信メールを送りましたのでご確認ください。<br>';
print 'ご注文いただいた商品は以下の住所に発送させていただきます。<br><br>';
print '入力された内容は以下の通りです。<br>';
print '<h4>お客様ご入力内容</h4>';
print '<table><tr><th>お名前</th>';
print '<td>'.$cname.'</td></tr>';
print '<tr><th>メールアドレス</th>';
print '<td>'.$email.'</td></tr>';
print '<tr><th>郵便番号</th>';
print '<td>'.substr($postal,0,3).'-'.substr($postal,3,4);
print '<tr><th>住所</th>';
print '<td>'.$address.'</td></tr>';
print '<tr><th>電話番号</th>';
print '<td>'.$tel.'</td></tr>';
print '<tr><th>備 考</th><td>'.nl2br($note).'</td></tr>';
print '<tr><th>会員登録</th><td>';
if($mem=='yes'){
  print '会員登録しました</td></tr>';
}else{
  print '今回注文のみです</td></tr>';
}
$date_time=new DateTime();
$date_time_disp=$date_time->format("Y-m-d H:i");
print '<tr><th>注文日時</th><td>'.$date_time_disp.'</td></tr>';
print '</table>';
if($mem=='yes'){
  print '会員登録していただきありがとうございます。<br>';
  print '次回からメールアドレスとパスワードで<br>';
  print 'ログインできます。';
  print 'ご注文が簡単にできるようになります。<br>';
}
$replymail='';
$replymail.=$cname." 様\n\nこのたびはご注文ありがとうございました。\n";
$replymail.="\n";
$replymail.="入力された内容は以下の通りです。\n";
$replymail.="\n";
$replymail.="お名前:".$cname."\n";
$replymail.="メールアドレス:".$email."\n";
$replymail.="郵便番号:".$postal."\n";
$replymail.="住所:".$address."\n";
$replymail.="電話番号:".$tel."\n";
$replymail.="備考:".$note."\n";
$replymail.="日時:".$date_time_disp."\n";
$replymail.="\n";
if($mem=='yes'){
$replymail.="会員登録していただきありがとうございます。\n";
$replymail.="次回からメールアドレスとパスワードでログインできます。\n";
$replymail.="ご注文が簡単にできるようになります。\n";
}
$replymail.="ご注文商品\n";
$replymail.="**************************\n";

$id=$_SESSION['id'];
$name=$_SESSION['name'];
$price=$_SESSION['price'];
$num=$_SESSION['num'];
$max=count($id);
print '<h4>ご注文商品内容</h4>';
print '<table><tr>';
print '<td>No.</td><td>商品名</td><td>価格</td>';
print '<td>数量</td><td>小計</td></tr>';
$sum=0; $sub=0;
for($i=0; $i<$max; $i++)
{
  $i1=$i+1;
  print '<tr><td>'.$i1.'</td><td>'.$name[$i].'</td>';
  print '<td class="right">'.number_format($price[$i]).'</td>';
  print '<td class="right">'.$num[$i].'</td>';
  $sub=$price[$i] * $num[$i];
  $sum += $sub;
  print '<td class="right">'.number_format($sub).'</td></tr>';
  $replymail.='No.'.$i1."\n";
  $replymail.='商品名:'.$name[$i]."\n";
  $replymail.='価格:'.number_format($price[$i])." 円\n";
  $replymail.='数量:'.$num[$i]."\n";
  $replymail.='小計:'.number_format($sub)." 円\n\n";
}
print '<tr><td colspan="4" class="right">合 計</td>';
print '<td class="right">'.number_format($sum).'</td></tr>';
print '<tr><td colspan="4" class="right">消費税</td>';
print '<td class="right">'.number_format($sum * 0.1).'</td></tr>';
print '<tr><td colspan="4" class="right">合計金額</td>';
print '<td class="right">'.number_format($sum * 1.1).'</td></tr>';
print '<table>';
$replymail.="合計:".number_format($sum)." 円\n";
$replymail.="消費税:".number_format($sum * 0.1)." 円\n";
$replymail.="税込金額:".number_format($sum * 1.1)." 円\n\n";
$replymail.="送料は別途かかります。\n";
$replymail.="**************************\n";
$replymail.="\n";
$replymail.="--------------------------\n";
$replymail.=" ネットショップ〇〇〇\n";
$replymail.="\n";
$replymail.="住所:〇〇〇〇〇〇〇〇〇〇〇〇〇\n";
$replymail.="電話:〇〇-〇〇〇-〇〇〇〇\n";
$replymail.="メール:〇〇@〇〇〇〇〇.com\n";
$replymail.="--------------------------\n";
print '<br>';

$title='ご注文ありがとうございます。';
$header='From:〇〇@〇〇〇〇〇.com';
$mail1=html_entity_decode($replymail,ENT_QUOTES,'UTF-8');
mb_language('Japanese');
mb_internal_encoding('UTF-8');
//mb_send_mail($email,$title,$mail1,$header);

$title='お客様からおご注文がありました。';
$header='From:'.$email;
$mail1=html_entity_decode($replymail,ENT_QUOTES,'UTF-8');
mb_language('Japanese');
mb_internal_encoding('UTF-8');
//mb_send_mail('〇〇@〇〇〇〇〇.com',$title,$mail1,$header);

try
{
$dbh=dbconnect();
$dbh->query('SET NAMES utf8');

$sql='LOCK TABLES cust_order,order_goods,cust_member WRITE';
$stmt=$dbh->prepare($sql);
$stmt->execute();

$lastmid=0;
if($mem=='yes')
{
  $sql='INSERT INTO cust_member(pass,name,email,postal,address,tel) VALUES(?,?,?,?,?,?)';
  $stmt=$dbh->prepare($sql);
  $data[]=md5($lpass);
  $data[]=$cname;
  $data[]=$email;
  $data[]=$postal;
  $data[]=$address;
  $data[]=$tel;
  $stmt->execute($data);

  $sql='SELECT LAST_INSERT_ID()';
  $stmt=$dbh->prepare($sql);
  $stmt->execute();
  $rec=$stmt->fetch(PDO::FETCH_ASSOC);
  $lastmid=$rec['LAST_INSERT_ID()'];
}

$sql='INSERT INTO cust_order(mid,name,email,postal,address,tel,note) VALUES(?,?,?,?,?,?,?)';
$stmt=$dbh->prepare($sql);
$data=array();
$data[]=$lastmid;
$data[]=$cname;
$data[]=$email;
$data[]=$postal;
$data[]=$address;
$data[]=$tel;
$data[]=$note;
$stmt->execute($data);

$sql='SELECT LAST_INSERT_ID()';
$stmt=$dbh->prepare($sql);
$stmt->execute();
$rec=$stmt->fetch(PDO::FETCH_ASSOC);
$lastid=$rec['LAST_INSERT_ID()'];

for($i=0; $i<$max; $i++)
{
  $sql='INSERT INTO order_goods(id,mid,name,price,num) VALUES(?,?,?,?,?)';
  $stmt=$dbh->prepare($sql);
  $data=array();
  $data[]=$lastid;
  $data[]=$id[$i];
  $data[]=$name[$i];
  $data[]=$price[$i];
  $data[]=$num[$i];
  $stmt->execute($data);
}

$sql='UNLOCK TABLES';
$stmt=$dbh->prepare($sql);
$stmt->execute();

$dbh=null;
}
catch(Exception $e)
{
  print ' ただいま障害により大変ご迷惑をおかけしております。';
  exit();
}
unset($_SESSION['id']);
unset($_SESSION['name']);
unset($_SESSION['price']);
unset($_SESSION['num']);

?>
<br>
<button class="btn1" onclick="location.href='shop_top.php'">トップページへ</button>
      </section>
    </main>
  </body>
</html>

41~42行目:[$mem][$lpass]変数の追加。
61~66行目:会員登録をしたかしていないかを表示。
71~76行目:会員登録のお礼。(画面表示)
90~94行目:会員登録のお礼。(メールに追加)
164行目:ロックするテーブルに会員登録用テーブルを追加。
169~186行目:会員登録されていればテーブルに登録。
190行目:[$data]変数配列を会員登録で使ったので初期化。

XAMPPphpmyadminで動作確認してみました。
[テーブル名:cust_member]

[テーブル名:cust_order]

どちらにも登録されました。

重複登録されていますね。
後で重複チェックを入れようと思います。

会員ログイン画面

会員ログイン画面を作ります。

[fshop]フォルダに[member]フォルダを作成し
そこに保存します。

ファイル名:cust_login.html

<記述例>

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>登録会員ログイン画面</title>
    <style type="text/css">
<!--
.txtbox{display: inline-block;margin: 10px;}
h5{background-color:mediumaquamarine;padding: 3px 10px;margin: 8px 0;}
form {background: honeydew; margin:10px; padding:10px 0; border:1px solid gray; display: inline-block;}
input {margin:0; border:1px solid lightgray;padding: 10px;background-color:ivory;}
.box{padding: 15px; margin: 20px 30px;border:1px solid lightgray;background: white;}
.box2{padding: 10px 5px; margin: 10px;}
.btn1{color: white;font-size: 16px; background: teal;padding: 10px 68px;margin: 10px auto;display: block;}
.btn2{color: white;font-size: 16px;background: chocolate;padding: 10px 68px;margin: 0 auto;display: block;}
-->
    </style>
  </head>
  <body>
    <main>
      <section class="txtbox">
    <form method="post" action="cust_login_chk.php">
    ○○○ショップ
    <h5>会員ログイン</h5>
      <div class="box">
        メールアドレス<br>
        <input type="email" name="email"><br><br>
        パスワード<br>
        <input type="password" name="pass"><br><br>
    <input type="submit" value="ログイン" class="btn1">
      </div>
    </form>
    <div class="box2">
      新規ご登録される方はこちらから<br>
      <button class="btn2" onclick="location.href='newmember.html'"> 会 員 登 録 </button>
    </div>
      </section>
    </main>
  </body>
</html>

<表示例>

特に解説はいらないと思います。
27行目:メールアドレスの入力。
29行目:パスワードを入力。
30行目:ログインボタン。
35行目:新規会員登録のボタン。移動先は[newmember.html]。

会員ログインチェック画面

会員ログインチェック画面を作ります。

ファイル名:cust_login_chk.php

<記述例>

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>登録会員ログイン画面</title>
    <style type="text/css">
<!--
.txtbox{display: inline-block;margin: 10px;}
.txtbox2{padding: 15px; margin: 10px;border:1px solid lightgray;background: white;}
.btn2{color: white;font-size: 16px;background: darkgray;padding: 5px 20px;margin: 10px auto;display: block;}
-->
    </style>
  </head>
  <body>
    <main>
      <section class="txtbox">
<?php

require_once('../com_func.php');
$post=sanitize($_POST);
$email=$post['email'];
$pass=$post['pass'];
$pass=md5($pass);

try
{
$dbh=dbconnect();
$dbh->query('SET NAMES utf8');

$sql='SELECT email FROM cust_member WHERE email=? AND pass=?';
$stmt=$dbh->prepare($sql);
$data[]=$email;
$data[]=$pass;
$stmt->execute($data);

$dbh=null;

$rec=$stmt->fetch(PDO::FETCH_ASSOC);

if($rec==false)
{
  print '<div class="txtbox2">';
  print 'ログインできません。<br>';
  print 'ユーザー名かパスワードが間違っています。<br>';
  print '<button class="btn2" onclick="location.href='."'cust_login.html'".'">戻 る</button>';
  print '</div>';
}
else
{
  session_start();
  $_SESSION['login']=true;
  $_SESSION['email']=$rec['email'];
  header('Location:../item_list.php');
}
}
catch(Exception $e)
{
  print ' ただいま障害により大変ご迷惑をおかけしております。';
  exit();
}

?>
      </section>
    </main>
  </body>
</html>

これも特に解説の必要ないでしょう。
27~36行目:データベース処理。
40行目:ログインしたメールアドレスとパスワードとが共に
    同じものがない場合、[$rec]はからなので偽(false)です。
41~47行目:ログインできない旨のエラー表示。
50行目:セッションスタート。
51行目:ログインできたことをセッションに保存。
52行目:メールアドレスもセッションに保存。
53行目:[item_list.php]に移動する。

会員ログインアウト画面

会員ログインアウト画面を作ります。

ファイル名:cust_logout.php

<記述例>

<?php
session_start();
$_SESSION=array();
if(isset($_COOKIE[session_name()])==true)
{
  setcookie(session_name(),'',time()-42000,'/');
}
@session_destroy();
?>
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>登録会員ログアウト</title>
  </head>
  <body>
    <main>
      <section>
      ログアウトしました。<br><br>
      <a href="../item_list.php">商品リストへ</a>
      </section>
    </main>
  </body>
</html>

<表示例>

20行目:商品リスト[item_list.php]に
    移動するようにリンクされています。

この一連のログイン・ログアウトについて、
詳しくは以下の記事をお読みください。

記 事 紹 介
PHPで会員登録にチャレンジ!【6.ログイン画面を作ろう】

ログイン・ログアウト画面を作成しました。

次は、ログイン画面の下にあった、
新規会員登録画面を作ります。

コメント

タイトルとURLをコピーしました