PHP自练项目中个人中心创建,修改,验证(服务器端和客户端验证)

时间:2022-07-03 04:41:38

当注册成功到登录后进入个人中心,查看和修改自己的资料

第一步:创建个人中心:

<?php
//定义个常量,用来授权调用includes里面的文件
define('IN_TG',true);
//定义个常量,用来指定本页的内容
define('SCRIPT','member');
//引入公共文件
require dirname(__FILE__).'/includes/common.inc.php';
//是否正常登录
if (isset($_COOKIE['username'])) {
//获取数据
$_rows=_fetch_array("SELECT tg_username,tg_sex,tg_face,tg_email,tg_url,tg_qq,tg_level,tg_reg_time FROM tg_user WHERE tg_username='{$_COOKIE['username']}'");
if($_rows){
/* $_username=$_rows['tg_username'];
$_sex=$_rows['tg_sex']; */
$_html=array();
$_html['username']=$_rows['tg_username'];
$_html['sex'] = $_rows['tg_sex'];
$_html['face'] = $_rows['tg_face'];
$_html['email'] = $_rows['tg_email'];
$_html['url'] = $_rows['tg_url'];
$_html['qq'] = $_rows['tg_qq'];
$_html['reg_time'] = $_rows['tg_reg_time'];
switch ($_rows['tg_level']){
case 0:
$_html['level'] = '普通会员';
break;
case 1:
$_html['level'] = '管理员';
break;
default:
$_html['level'] = '不是本地管理员';
}
$_html = _html($_html);
}else{
_alert_back('此用户不存在!');
}
}else{
_alert_back("非法登录!");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>多用户留言系统--个人用户中心</title>
<?php
require ROOT_PATH.'includes/title.inc.php';
?>
</head>
<body>
<?php
require ROOT_PATH.'includes/header.inc.php';
?>
<div id="member">
<?php
require ROOT_PATH.'includes/member.inc.php';
?>
<div id="member_main">
<h2>会员管理中心</h2>
<dl>
<dd>用户名:<?php echo $_html['username']?></dd>
<dd>性&nbsp;&nbsp;别:<?php echo $_html['sex']?></dd>
<dd>头&nbsp;&nbsp;像:<?php echo $_html['face']?></dd>
<dd>电子邮件:<?php echo $_html['email']?></dd>
<dd>主&nbsp;&nbsp;页:<?php echo $_html['url']?></dd>
<dd>QQ&nbsp;&nbsp;:<?php echo $_html['qq']?></dd>
<dd>注册时间:<?php echo $_html['reg_time']?></dd>
<dd>身&nbsp;&nbsp;份:<?php echo $_html['level']?></dd>
</dl>
</div>
</div>
<?php
require ROOT_PATH.'includes/footer.inc.php';
?> </body>
</html>
 require ROOT_PATH.'includes/member.inc.php'中的代码:
<?php
//防止恶意调用
if (!defined('IN_TG')) {
exit('Access Defined!');
} ?>
<div id="member_sidebar">
<h2>中心导航</h2>
<dl>
<dt>帐号管理</dt>
<dt>
<a href="member.php">个人信息</a>
</dt>
<dt>
<a href="member_modify.php">修改资料</a>
</dt>
</dl>
<dl>
<dt>其他管理</dt>
<dt>
<a href="#">短信查阅</a>
</dt>
<dt>
<a href="#">好友设置</a>
</dt>
<dt>
<a href="#">查询花朵</a>
</dt>
<dt>
<a href="#">个人相册</a>
</dt>
</dl>
</div>

相关的CSS

@CHARSET "UTF-8";
#member {
width:auto;
height:600px;
border:2px solid #ccc;
font-size:12px;
}
#member h2 {
text-indent:;
text-align:center;
height:30px;
line-height:30px;
}
#member #member_sidebar {
width:300px;
height:500px;
margin:15px;
border:1px dashed #999;
float:left;
}
#member #member_sidebar dl {
width:250px;
background:red;
margin:15px auto;
}
#member #member_sidebar dl dt {
height:40px;
line-height:40px;
background:#999;
font-weight:bold;
text-align:center;
color:#fff;
}
#member #member_sidebar dl dd {
height:40px;
line-height:40px;
background:#eee;
font-weight:bold;
text-align:center;
}
#member #member_sidebar dl dd a {
display:block;
color:#333;
text-decoration:none;
}
#member #member_sidebar dl dd a:hover {
background:#ccc;
}
#member #member_main {
width:590px;
height:500px;
margin:15px;
border:1px dashed #999;
float:right;
}
#member #member_main dl {
width:500px;
margin:15px auto;
font-size:14px;
}
#member #member_main dl dd {
padding:10px 0;
border-bottom:1px dashed #999;
}

个人中心创建后就是修改个人中心:

<?php
session_start();
//定义个常量,用来授权调用includes里面的文件
define('IN_TG',true);
//定义个常量,用来指定本页的内容
define('SCRIPT','member_modify');
//引入公共文件
require dirname(__FILE__).'/includes/common.inc.php'; //修改资料及服务端的验证函数是调用了注册时使用的register.func.php 来验证
if($_GET['action']=='modify'){
//echo '修改的资料有误,请确认';
_check_code($_POST, $_SESSION['code']);
include ROOT_PATH.'includes/register.func.php';
$_clean=array();
$_clean['password'] = _check_modify_password($_POST['password'],6);
$_clean['sex']=_check_sex($_POST['sex']);
$_clean['face'] = _check_face($_POST['face']);
$_clean['email'] = _check_email($_POST['email'], 6, 40);
$_clean['qq'] = _check_qq($_POST['qq']);
$_clean['url'] = _check_url($_POST['url'], 40);
//修改资料
if (empty($_clean['password'])){
_query("UPDATE tg_user SET
tg_password='{$_clean['password']}',
tg_sex='{$_clean['sex']}',
tg_face='{$_clean['face']}',
tg_email='{$_clean['email']}',
tg_qq='{$_clean['qq']}';
tg_url='{$_clean['url']}'
WHERE
tg_username='{$_COOKIE['username']}'
"); }else{
_query("UPDATE tg_user SET
tg_password='{$_clean['password']}',
tg_sex='{$_clean['sex']}',
tg_face='{$_clean['face']}',
tg_email='{$_clean['email']}',
tg_qq='{$_clean['qq']}',
tg_url='{$_clean['url']}'
WHERE
tg_username='{$_COOKIE['username']}'
");
}
//判断是否修改
if(_affected_rows()==1){
_close();
_session_destroy();
_location('恭喜你,修改成功', 'member.php');
}else{
_close();
_session_destroy();
_location('很遗憾,修改失败', 'member_modify.php');
} }
//是否正常登录
if (isset($_COOKIE['username'])) {
//获取数据
$_rows=_fetch_array("SELECT tg_username,tg_sex,tg_face,tg_email,tg_url,tg_qq FROM tg_user WHERE tg_username='{$_COOKIE['username']}'");
if($_rows){
/* $_username=$_rows['tg_username'];
$_sex=$_rows['tg_sex']; */
$_html=array();
$_html['username']=$_rows['tg_username'];
$_html['sex'] = $_rows['tg_sex'];
$_html['face'] = $_rows['tg_face'];
$_html['email'] = $_rows['tg_email'];
$_html['url'] = $_rows['tg_url'];
$_html['qq'] = $_rows['tg_qq'];
$_html = _html($_html);
//性别选择
if ($_html['sex'] == '男') {
$_html['sex_html'] = '<input type="radio" name="sex" value="男" checked="checked" /> 男 <input type="radio" name="sex" value="女" /> 女';
} elseif ($_html['sex'] == '女') {
$_html['sex_html'] = '<input type="radio" name="sex" value="男" /> 男 <input type="radio" name="sex" value="女" checked="checked" /> 女';
}
//头像选择
$_html['face_html'] = '<select name="face">';
foreach (range(1,9) as $_num) {
$_html['face_html'] .= '<option value="face/m0'.$_num.'.gif">face/m0'.$_num.'.gif</option>';
}
foreach (range(10,64) as $_num) {
$_html['face_html'] .= '<option value="face/m'.$_num.'.gif">face/m'.$_num.'.gif</option>';
}
$_html['face_html'] .= '</select>'; }else{
_alert_back('此用户不存在!');
}
}else{
_alert_back("非法登录!");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>多用户留言系统--个人用户中心</title>
<?php
require ROOT_PATH.'includes/title.inc.php';
?>
<script type="text/javascript" src="js/code.js"></script>
<script type="text/javascript" src="js/member_modify.js"></script>
</head>
<body>
<?php
require ROOT_PATH.'includes/header.inc.php';
?>
<div id="member">
<?php
require ROOT_PATH.'includes/member.inc.php';
?>
<div id="member_main">
<h2>会员管理中心</h2>
<form method="post" action="?action=modify">
<dl>
<dd>用户名:<?php echo $_html['username']?></dd>
<dd>密 码:<input type="password" class="text" name="password"/>(*留空表示不修改)</dd>
<dd>性&nbsp;&nbsp;别:<?php echo $_html['sex_html']?></dd>
<dd>头&nbsp;&nbsp;像:<?php echo $_html['face_html']?></dd>
<dd>电子邮件:<input type="text" class="text" name="email" value="<?php echo $_html['email']?>" /></dd>
<dd>主&nbsp;&nbsp;页:<input type="text" class="text" name="url" value="<?php echo $_html['url']?>" /></dd>
<dd>QQ&nbsp;&nbsp;:<input type="text" class="text" name="qq" value="<?php echo $_html['qq']?>" /></dd>
<dd>验证码:<input type="text" name="code" class="text yzm" /><img src="code.php" id="code"></img></dd>
<dd><input type="submit" class="submit" value="修改资料" /></dd>
</dl>
</form>
</div>
</div>
<?php
require ROOT_PATH.'includes/footer.inc.php';
?> </body>
</html>

_check_modify_password:

相对应的封装函数:

/**
*
* @param unknown $_string
* @param unknown $_min_num
* @return NULL|string
*/
function _check_modify_password($_string,$_min_num){
//判断密码
if (! empty ( $_string )) {
if (strlen ( $_string ) < $_min_num) {
_alert_back ( '密码不得小于' . $_min_num . '位!' );
}
}else{
return null;
}
return sha1($_string);
}

相关的CSS文件:

@CHARSET "UTF-8";
#member {
width:auto;
height:600px;
border:2px solid #ccc;
font-size:12px;
}
#member h2 {
text-indent:;
text-align:center;
height:30px;
line-height:30px;
}
#member #member_sidebar {
width:300px;
height:500px;
margin:15px;
border:1px dashed #999;
float:left;
}
#member #member_sidebar dl {
width:250px;
background:red;
margin:15px auto;
}
#member #member_sidebar dl dt {
height:40px;
line-height:40px;
background:#999;
font-weight:bold;
text-align:center;
color:#fff;
}
#member #member_sidebar dl dd {
height:40px;
line-height:40px;
background:#eee;
font-weight:bold;
text-align:center;
}
#member #member_sidebar dl dd a {
display:block;
color:#333;
text-decoration:none;
}
#member #member_sidebar dl dd a:hover {
background:#ccc;
}
#member #member_main {
width:590px;
height:500px;
margin:15px;
border:1px dashed #999;
float:right;
}
#member #member_main dl {
width:500px;
margin:15px auto;
font-size:14px;
}
#member #member_main dl dd {
padding:10px 0;
border-bottom:1px dashed #999;
}
#member #member_main dl dd input.text {
width:220px;
height:19px;
border:1px dashed #333;
background:#fff;
}
#member #member_main dl dd input.yzm {
width:60px;
}
#member #member_main dl dd img#code {
position:relative;
top:8px;
cursor:pointer;
}
#member #member_main dl dd input.submit {
width:60px;
height:22px;
border:1px dashed #333;
background:#fff;
cursor:pointer;
}

客户端验证:

调用了register.js 的代码

window.onload=function(){
code();
//表单验证,客户端
var fm=document.getElementsByTagName('form')[0];
fm.onsubmit=function(){
//密码验证
if(fm.password.value=''){
if (fm.password.value.length < 6) {
alert('密码不得小于6位');
fm.password.value = ''; //清空
fm.password.focus(); //将焦点以至表单字段
return false;
}
}
//邮箱验证
if (!/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/.test(fm.email.value)) {
alert('邮件格式不正确');
fm.email.value = ''; //清空
fm.email.focus(); //将焦点以至表单字段
return false;
}
//QQ号码
if (fm.qq.value != '') {
if (!/^[1-9]{1}[\d]{4,9}$/.test(fm.qq.value)) {
alert('QQ号码不正确');
fm.qq.value = ''; //清空
fm.qq.focus(); //将焦点以至表单字段
return false;
}
} //网址
if (fm.url.value != '') {
if (!/^https?:\/\/(\w+\.)?[\w\-\.]+(\.\w+)+$/.test(fm.url.value)) {
alert('网址不合法');
fm.url.value = ''; //清空
fm.url.focus(); //将焦点以至表单字段
return false;
}
}
//验证码验证
if (fm.code.value.length != 4) {
alert('验证码必须是4位');
fm.code.value = ''; //清空
fm.code.focus(); //将焦点以至表单字段
return false;
}
};
};