如何用PHP搭建会员注册及登陆系统(2)

Activate.Php

这个页面自动检查网址中所传入的用户的id和激活码(用户点击右键中的链接后),如果信息与数据库中存储的内容一致,将自动更新数据库中激活码一栏为Yes。

    <?php
    require('includes/config.php');

    //collect values from the url
    $memberID = trim($_GET['x']);
    $active = trim($_GET['y']);

    //if id is number and the active token is not empty carry on
    if(is_numeric($memberID) && !empty($active)){

        //update users record set the active column to Yes where the memberID and active value match the ones provided in the array
        $stmt = $db->prepare("UPDATE members SET active = 'Yes' WHERE memberID = :memberID AND active = :active");
        $stmt->execute(array(
            ':memberID' => $memberID,
            ':active' => $active
        ));

        //if the row was updated redirect the user
        if($stmt->rowCount() == 1){

            //redirect to login page
            header('Location: login.php?action=active');
            exit;

        } else {
            echo "Your account could not be activated.";
        }

    }
    ?>
继续阅读

利用mb_substr函数解决PHP截取汉字乱码的问题

学习php过程中遇到一个substr函数,但是截取中文时一直乱码,刚开始以为是数据库编码输出问题,后来百度下发现了解决方案,就是mb_substr函数,要想使用这个函数,还需要做一点准备工作,具体步骤如下:

  1. 检查你的Windows/system32下是否有php_mbstring.dll这个文件,没有的话就差那个PHP安装目录中extensions文件夹复制到Windows/system32文件夹中。
  2. 打开PHP配置文件php.ini,搜索mbstring.dll,找到

    ;extension=php_mbstring.dll,去掉前面的;分号,这样mb_substr函数就可以使用了。
    mb_strcut函数功能也可以截取字符串长度,下面就给大家演示下具体区别:

<pre class="line-numbers prism-highlight" data-start="1"><code class="language-php"><?php
$str = '这样一来我的字符串就不会有乱码^_^'; 

echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8');
//结果:这样一来我的字
echo "<br>"; 

echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
//结果:这样
?> 

从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象。

如何用PHP搭建会员注册及登陆系统(1)

之前用勤哲EXCEL服务器给公司搭建了一个简单的ERP系统,后来发现局限性还是非常大的,决定自学PHP搭建一个完整的系统,平时就学习Tutsplus的PHP Fundamentals视频教程,然后买了一本兄弟连的《细说PHP》一书配合,受益匪浅。

废话不多说,搭建网站的第一步当然是要有一个权限管理,即会员注册登陆,在Google上看到一篇不错的教程,今天就翻译给大家看。这篇教程将会教授大家用PHP的OOP特性制作一个会员注册、激活、找回密码的简单系统,如果大家有更好的方法,也欢迎留言分享。

源代码下载: https://github.com/daveismynamecom/loginregister 系统构架如下图所示:

继续阅读