---
# System prepended metadata

title: 蕭幫1219PHP教學
tags: [PHP]

---

###### tags: `PHP`

# 蕭幫1219PHP教學
# 實作 註冊→登入→會員中心

# 資料庫
資料庫名稱 phpclass

表單 user

ID int(5) AUTO_INCREMENT
account varchar(25) utf8_general_ci
name varchar(5)
nickname varchar(10)
sex char(1)
birthday date
mail varchar(25)
address varchar(50)
img varchar(255)

表單 detail

account varchar(25)
password varchar(18)
uni varchar(255)
active varchar(2)
time date

# Register.php

- 一、將要傳值的表單完成
    - uniqid() 以為秒計的當前時間，生成一個唯一的ID
```php=
<?php
$uni = sha1(uniqid(rand(),true)); //唯一標識符
?>
<input type="hidden" name="uni" value="<?php echo $uni;?>" />
```

- 二、到main.php準備接資料

```php=
if(isset($_POST['register'])){	/*註冊*/


	/*********接收資料***********/

	$name = $_POST['name'];
	$nickname = $_POST['nickname'];
	$account = $_POST['account'];
	$password = $_POST['password'];
	$checkPassword = $_POST['checkPassword'];
	$sex = $_POST['sex'];
	$birthday=$_POST['year'].'/'.$_POST['month'].'/'.$_POST['day'];	/*將日期合併*/
	$mail = $_POST['mail'];
	$address = $_POST['address'];
	$uni = $_POST['uni'];
	$time = date("Y-m-d",time());

	/******************************/



	/********* 將資料寫進資料庫 *********/

	$sql1="INSERT INTO user(`account`,`name`,`nickname`,`sex`,`birthday`,`mail`,`address`,`img`)
			VALUES('".$account."','".$name."','".$nickname."','".$sex."','".$birthday."','".$mail."','".$address."','')";

	$sql2="INSERT INTO detail(`account`,`password`,`uni`,`active`,`time`)
			VALUES ('".$account."','".$password."','".$uni."','','".$time."')";
	filterTable($sql1);
	filterTable($sql2);
	_alert('註冊成功!','login.php');

	/*************************************/


	}
```

# Login.php

- 一、將要傳出的表單完成
    - checkCode.php
        - 產生一組驗證碼圖片
    - SESSION 全域變數
        - 基本用法
```php=
session_start();
$_SESSION['test'] = 'abc';
```
- 二、到main.php準備接資料
```php=
if(isset($_POST['login'])){		/*登入*/
	/*接收資料*/
	$account = $_POST['account'];
	$pwd = $_POST['password'];
	$check = $_POST['check'];
	/**********/

	/******* 登入判斷 ******/
	$SQL1="SELECT `account` FROM `detail` WHERE `account` = '".$account."' AND `password` ='".$pwd."'"; /*與資料庫核對有無此帳號與密碼*/

	if($check==$_SESSION['checkNum']){
		$result=_fetch_array($SQL1);
		if($result!=""){
			$_SESSION['user'] = $account;
			_alert('登入成功!','member.php');
		}else{
			_alert('登入失敗!','login.php');
		}
	}else{
		_alert('驗證碼錯誤!','login.php');
	}
	/*************************/
	
	_alert('登入成功!','member.php');
	// print_r($result);

}
	
```

# member.php
- 此處要將資料庫中會員的資料撈出進行顯示
```php=
include "db_connect.php";
$sql = "SELECT a.*,b.`uni`,b.`active` 
        FROM `user` as a ,`detail` as b 
        WHERE a.`account` ='{$_SESSION["user"]}' 
        and b.`account` ='{$_SESSION["user"]}' ";
        
$result=_fetch_array($sql);
$img=$result['img'];            //圖片位置
$active = $result['active'];    //認證狀態
```
- 接著將撈出的資料放進相對應的欄位，例如:
```html5=
<label for="" class="col-form-label-lg info"><?php echo $result['name'];?></label>
<input name="name" type="text" class="form-control toChange" placeholder="Name" value="<?php echo $result['name'];?>" >
```
- 更新資料，可以更新的欄位為【姓名】、【暱稱】、【E-mail】、【地址】
```php=
if(isset($_POST['update'])){		/*修改個人資料*/

/*********接收資料***********/

$name = $_POST['name'];
$nickname = $_POST['nickname'];
$email = $_POST['mail'];
$address = $_POST['address'];

/****************************/

/********** 更新資料庫內的資料 **********/

$sql = "UPDATE `user` SET
    `name` = '$name',
    `nickname` = '$nickname',
    `mail` = '$email',
    `address` = '$address'
    WHERE `account` = '".$_SESSION['user']."'";

filterTable($sql);
/**************************************/
}
```
- 對有無上傳大頭貼進行判斷，為避免在HTML中嵌入太多PHP程式碼導致不易閱讀，將之寫為一個function後，在html中進行呼叫
```php=
function imgs($img){
    if($img==""){
        echo 'defult.png';
    }else{
        echo $img.'.jpg';
    }
}
```
- 在HTML中呼叫並帶值
```html5=
<img src="images/<?php imgs($img); ?>" style="width:200px; height:200px; margin:-200 0 0 30;">
```
- 上傳圖片
需要上傳圖片的表單需要將form改成如下
```html5=
 <form method="POST" action="main.php" enctype="multipart/form-data">
```
並在要顯示上傳圖片按鈕的位置輸入
```html5=
<input name="upfile[]" type="file" multiple="multiple" id="file">
```
後端處理
```php=
/************* 上傳圖片 ***************/
$type=$_FILES['upfile']['type'];    //取的上傳圖片的名稱以及檔案類型

$sql1 = "SELECT `uni` FROM `detail` WHERE `account` = '".$_SESSION['user']."'";
$uni=_fetch_array($sql1);            //取出會員編碼，準備作為圖片新的名稱

$i=0;
while($i<count($_FILES['upfile']['name'])){    //上傳多張圖片

$newname="img_".$uni[0].$i;                    //定義新名稱

move_uploaded_file($_FILES['upfile']['tmp_name'[$i],"images/".$newname.".jpg");    //將使用者選定的圖片移動至指定位置

$sqll = "UPDATE `user` set `img` = '$newname' WHERE `account` = '".$_SESSION['user']."'";                      //儲存新的圖片名稱
$result = filterTable($sqll);    
$i++;
}
/************************************/
```
- 對有無進行信箱認證進行判斷，方式同判斷大頭貼
```php=
function act($active){
if($active==""){
echo '<a href="active.php"> <label for="" class="col-form-label-lg ">未認證</label></a>';
}else{
echo '<label for="" class="col-form-label-lg ">已認證</label>';
    }
}
```
- 在HTML中呼叫並帶值
```html5=
<div class="form-group row p-3">
<label for="address" class="col-sm-3 col-form-label-lg">信箱認證狀態</label>
<div class="col">
<?php act($result['active']); ?>
</div>
</div>
```
- 登出&刪除
```php=
if(isset($_POST['logout'])){  /*登出*/
session_destroy();
_alert('登出成功!','login.php');
}

if(isset($_POST['del'])){  /*刪除*/

$sql1 = "DELETE FROM `user` WHERE `account` = '{$_SESSION["user"]}'";
$sql2 = "DELETE FROM `detail`WHERE `account` = '{$_SESSION["user"]}'";
filterTable($sql1);
filterTable($sql2);
session_destroy();
_alert('刪除成功!','login.php');
}
```


認證信箱 active.php
===
```php=
include "db_connect.php";

if($_GET['active']!=""){
$sql="SELECT `uni` FROM `detail` WHERE `account` = '".$_SESSION['user']."'";
$code = _fetch_array($sql);
if($code[0]!=$_GET['active']){
_alert('認證失敗!,請重新操作','member.php');
}else{
$sql1="UPDATE `detail` set `active`='ok' WHERE `account` = '".$_SESSION['user']."'";
filterTable($sql1);
_alert('認證成功','member.php');
}
}else{

$sql="SELECT a.`uni`, b.`mail` FROM `detail` as a , `user` as b WHERE a.`account` = '".$_SESSION['user']."'";
$result=_fetch_array($sql);
$ver='http://'.$_SERVER["HTTP_HOST"].substr($_SERVER['PHP_SELF'],0,9).'/active.php'.'?active='.$result[0];
$mail=$result[1];

mailer($mail,$ver);
}
```


        
    



    
    





# 修改XAMPP的mysql密碼
C:\xampp\phpMyAdmin\config.inc.php

```php=
/* Authentication type and info */

$cfg['Servers'][$i]['auth_type'] = 'http'; <-改成http

$cfg['Servers'][$i]['user'] = 'root';

$cfg['Servers'][$i]['password'] = '●●●●●●';   <-填入剛剛輸入的密碼

$cfg['Servers'][$i]['extension'] = 'mysqli';

$cfg['Servers'][$i]['AllowNoPassword'] = true;

$cfg['Lang'] = '';
```

# Dark Theme CSS

{%hackmd theme-dark %}