PHP会员找回密码功能实现实例介绍

 更新时间:2016年11月25日 17:26  点击:1781
如果你做的网站有会员系统那必须就自动找回密码功能了,也就是忘记密码功能,如果用户忘记了密码可以通过邮箱或手机号直接找回密码,下面我来介绍邮箱找回密码方法。

设置思路

1、用户注册时需要提供一个E-MAIL邮箱,目的就是用该邮箱找回密码。

2、当用户忘记密码或用户名时,点击登录页面的“找回密码”超链接,打开表单,并输入注册用的E-MAIL邮箱,提交。

3、系统通过该邮箱,从数据库中查找到该用户信息,并更新该用户的密码为一个临时密码(比如:12345678)。

4、系统借助Jmail功能把该用户的信息发送到该用户的邮箱中(内容包括:用户名、临时密码、提醒用户及时修改临时密码的提示语)。

5、用户用临时密码即可登录。


HTML

我们在找回密码的页面上放置一个要求用户输入注册时所用的邮箱,然后提交前台js来处理交互。

 代码如下 复制代码

<p><strong>输入您注册的电子邮箱,找回密码:</strong></p>
<p><input type="text" class="input" name="email" id="email"><span id="chkmsg"></span></p>
<p><input type="button" class="btn" id="sub_btn" value="提 交"></p>

jQuery

当用户输入完邮箱并点击提交后,jQuery先验证邮箱格式是否正确,如果正确则通过向后台sendmail.php发送Ajax请求,sendmail.php负责验证邮箱是否存在和发送邮件,并会返回相应的处理结果给前台页面,请看jQuery代码:

 代码如下 复制代码

$(function(){
    $("#sub_btn").click(function(){
        var email = $("#email").val();
        var preg = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/; //匹配Email
        if(email=='' || !preg.test(email)){
            $("#chkmsg").html("请填写正确的邮箱!");
        }else{
            $("#sub_btn").attr("disabled","disabled").val('提交中..').css("cursor","default");
            $.post("sendmail.php",{mail:email},function(msg){
                if(msg=="noreg"){
                    $("#chkmsg").html("该邮箱尚未注册!");
                    $("#sub_btn").removeAttr("disabled").val('提 交').css("cursor","pointer");
                }else{
                    $(".demo").html("<h3>"+msg+"</h3>");
                }
            });
        }
    });
})

以上使用的jQuery代码很方便简洁的完成了前端交互操作,如果您有一定的jQuery基础,那上面的代码一目了然,不多解释。

当然别忘了在页面中加载jQuery库文件,有的同学经常问我说从www.111cn.net下载了demo怎么用不了,那80%是jquery或者其他文件加载路径错了导致没加载必要的文件。
PHP

sendmail.php需要验证Email是否存在系统用户表中,如果有,则读取用户信息,将用户id、用户名和密码惊醒md5加密生成一个特别的字符串作为找回密码的验证码,然后构造URL。同时我们为了控制URL链接的时效性,将记录用户提交找回密码动作的操作时间,最后调用邮件发送类发送邮件到用户邮箱,发送邮件类smtp.class.php已经打包好,请下载。

 代码如下 复制代码

include_once("connect.php");//连接数据库
 
$email = stripslashes(trim($_POST['mail']));
    
$sql = "select id,username,password from `t_user` where `email`='$email'";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
if($num==0){//该邮箱尚未注册!
    echo 'noreg';
    exit;    
}else{
    $row = mysql_fetch_array($query);
    $getpasstime = time();
    $uid = $row['id'];
    $token = md5($uid.$row['username'].$row['password']);//组合验证码
    $url = "/demo/resetpass/reset.php?email=".$email."
&token=".$token;//构造URL
    $time = date('Y-m-d H:i');
    $result = sendmail($time,$email,$url);
    if($result==1){//邮件发送成功
        $msg = '系统已向您的邮箱发送了一封邮件<br/>请登录到您的邮箱及时重置您的密码!';
        //更新数据发送时间
        mysql_query("update `t_user` set `getpasstime`='$getpasstime' where id='$uid '");
    }else{
        $msg = $result;
    }
    echo $msg;
}
 
//发送邮件
function sendmail($time,$email,$url){
    include_once("smtp.class.php");
    $smtpserver = ""; //SMTP服务器,如smtp.163.com
    $smtpserverport = 25; //SMTP服务器端口
    $smtpusermail = ""; //SMTP服务器的用户邮箱
    $smtpuser = ""; //SMTP服务器的用户帐号
    $smtppass = ""; //SMTP服务器的用户密码
    $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); 
    //这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $emailtype = "HTML"; //信件类型,文本:text;网页:HTML
    $smtpemailto = $email;
    $smtpemailfrom = $smtpusermail;
    $emailsubject = "www.111cn.net - 找回密码";
    $emailbody = "亲爱的".$email.":<br/>您在".$time."提交了找回密码请求。请点击下面的链接重置密码
(按钮24小时内有效)。<br/><a href='".$url."'target='_blank'>".$url."</a>";
    $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
 
    return $rs;
}

好了,这个时候你的邮箱将会收到一封来自helloweba的密码找回邮件,邮件内容中有一个URL链接,点击该链接到www.111cn.net的reset.php来验证邮箱。

 代码如下 复制代码

include_once("connect.php");//连接数据库
 
$token = stripslashes(trim($_GET['token']));
$email = stripslashes(trim($_GET['email']));
$sql = "select * from `t_user` where email='$email'";
 
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
if($row){
    $mt = md5($row['id'].$row['username'].$row['password']);
    if($mt==$token){
        if(time()-$row['getpasstime']>24*60*60){
            $msg = '该链接已过期!';
        }else{
            //重置密码...
            $msg = '请重新设置密码,显示重置密码表单,<br/>这里只是演示,略过。';
        }
    }else{
        $msg =  '无效的链接';
    }
}else{
    $msg =  '错误的链接!';    
}
echo $msg;

reset.php首先接受参数email和token,然后根据email查询数据表t_user中是否存在该Email,如果存在则获取该用户的信息,并且和sendmail.php中的token组合方式一样构建token值,然后与url传过来的token进行对比,如果当前时间与发送邮件时的时间相差超过24小时的,则提示“该链接已过期!”,反之,则说明链接有效,并且调转到重置密码页面,最后就是用户自己设置新密码了。

小结:通过注册邮箱验证与本文邮件找回密码,我们知道发送邮件在网站开发中的应用以及它的重要性,当然,现在也流行短信验证应用,这个需要相关的短信接口对接就可以了。

最后,附上数据表t_user结构:

 代码如下 复制代码

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(32) NOT NULL,
  `email` varchar(50) NOT NULL,
  `getpasstime` int(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

smtp.class.php类文件

 代码如下 复制代码

<?php

class Smtp{

    /* Public Variables */

 var $smtp_port;

 var $time_out;

 var $host_name;

 var $log_file;

 var $relay_host;

 var $debug;

 var $auth;

 var $user;

 var $pass;

 /* Private Variables */
 var $sock;

 /* Constractor */

 function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) {
  $this->debug = false;

  $this->smtp_port = $smtp_port;

  $this->relay_host = $relay_host;

  $this->time_out = 30; //is used in fsockopen()

  $this->auth = $auth; //auth

  $this->user = $user;

  $this->pass = $pass;

  $this->host_name = "localhost"; //is used in HELO command
  $this->log_file = "";

  $this->sock = false;
 }

 /* Main Function */

 function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
  $mail_from = $this->get_address($this->strip_comment($from));

  $body = ereg_replace("(^|(rn))(.)", "1.3", $body);

  $header .= "MIME-Version:1.0rn";

  if ($mailtype == "HTML") {
   $header .= "Content-Type:text/htmlrn";
  }

  $header .= "To: " . $to . "rn";

  if ($cc != "") {
   $header .= "Cc: " . $cc . "rn";
  }

  $header .= "From: $from<" . $from . ">rn";

  $header .= "Subject: " . $subject . "rn";

  $header .= $additional_headers;

  $header .= "Date: " . date("r") . "rn";

  $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")rn";

  list ($msec, $sec) = explode(" ", microtime());

  $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">rn";

  $TO = explode(",", $this->strip_comment($to));

  if ($cc != "") {
   $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  }

  if ($bcc != "") {
   $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  }

  $sent = true;

  foreach ($TO as $rcpt_to) {
   $rcpt_to = $this->get_address($rcpt_to);

   if (!$this->smtp_sockopen($rcpt_to)) {
    $this->log_write("Error: Cannot send email to " . $rcpt_to . "n");

    $sent = false;

    continue;
   }

   if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
    $this->log_write("E-mail has been sent to <" . $rcpt_to . ">n");
   } else {
    $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">n");

    $sent = false;
   }

   fclose($this->sock);

   $this->log_write("Disconnected from remote hostn");
  }

  return $sent;
 }

 /* Private Functions */

 function smtp_send($helo, $from, $to, $header, $body = "") {
  if (!$this->smtp_putcmd("HELO", $helo)) {
   return $this->smtp_error("sending HELO command");
  }
  // auth
  if ($this->auth) {
   if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
    return $this->smtp_error("sending HELO command");
   }

   if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
    return $this->smtp_error("sending HELO command");
   }
  }

  if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
   return $this->smtp_error("sending MAIL FROM command");
  }

  if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
   return $this->smtp_error("sending RCPT TO command");
  }

  if (!$this->smtp_putcmd("DATA")) {
   return $this->smtp_error("sending DATA command");
  }

  if (!$this->smtp_message($header, $body)) {
   return $this->smtp_error("sending message");
  }

  if (!$this->smtp_eom()) {
   return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  }

  if (!$this->smtp_putcmd("QUIT")) {
   return $this->smtp_error("sending QUIT command");
  }

  return true;
 }

 function smtp_sockopen($address) {
  if ($this->relay_host == "") {
   return $this->smtp_sockopen_mx($address);
  } else {
   return $this->smtp_sockopen_relay();
  }
 }

 function smtp_sockopen_relay() {
  $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "n");

  $this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);

  if (!($this->sock && $this->smtp_ok())) {
   $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "n");

   $this->log_write("Error: " . $errstr . " (" . $errno . ")n");

   return false;
  }

  $this->log_write("Connected to relay host " . $this->relay_host . "n");

  return true;
  ;
 }

 function smtp_sockopen_mx($address) {
  $domain = ereg_replace("^.+@([^@]+)$", "1", $address);

  if (!@ getmxrr($domain, $MXHOSTS)) {
   $this->log_write("Error: Cannot resolve MX "" . $domain . ""n");

   return false;
  }

  foreach ($MXHOSTS as $host) {
   $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "n");

   $this->sock = @ fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);

   if (!($this->sock && $this->smtp_ok())) {
    $this->log_write("Warning: Cannot connect to mx host " . $host . "n");

    $this->log_write("Error: " . $errstr . " (" . $errno . ")n");

    continue;
   }

   $this->log_write("Connected to mx host " . $host . "n");

   return true;
  }

  $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")n");

  return false;
 }

 function smtp_message($header, $body) {
  fputs($this->sock, $header . "rn" . $body);

  $this->smtp_debug("> " . str_replace("rn", "n" . "> ", $header . "n> " . $body . "n> "));

  return true;
 }

 function smtp_eom() {
  fputs($this->sock, "rn.rn");

  $this->smtp_debug(". [EOM]n");

  return $this->smtp_ok();
 }

 function smtp_ok() {
  $response = str_replace("rn", "", fgets($this->sock, 512));

  $this->smtp_debug($response . "n");

  if (!ereg("^[23]", $response)) {
   fputs($this->sock, "QUITrn");

   fgets($this->sock, 512);

   $this->log_write("Error: Remote host returned "" . $response . ""n");

   return false;
  }

  return true;
 }

 function smtp_putcmd($cmd, $arg = "") {
  if ($arg != "") {
   if ($cmd == "")
    $cmd = $arg;

   else
    $cmd = $cmd . " " . $arg;
  }

  fputs($this->sock, $cmd . "rn");

  $this->smtp_debug("> " . $cmd . "n");

  return $this->smtp_ok();
 }

 function smtp_error($string) {
  $this->log_write("Error: Error occurred while " . $string . ".n");

  return false;
 }

 function log_write($message) {
  $this->smtp_debug($message);

  if ($this->log_file == "") {
   return true;
  }

  $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;

  if (!@ file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) {
   $this->smtp_debug("Warning: Cannot open log file "" . $this->log_file . ""n");

   return false;
   ;
  }

  flock($fp, LOCK_EX);

  fputs($fp, $message);

  fclose($fp);

  return true;
 }

 function strip_comment($address) {
  $comment = "([^()]*)";

  while (ereg($comment, $address)) {
   $address = ereg_replace($comment, "", $address);
  }

  return $address;
 }

 function get_address($address) {
  $address = ereg_replace("([ trn])+", "", $address);

  $address = ereg_replace("^.*<(.+)>.*$", "1", $address);

  return $address;
 }

 function smtp_debug($message) {
  if ($this->debug) {
   echo $message . "
   ;";
  }
 }
}
?>

最后面有个数据库连接类,这里就不介绍了大大家可以百本站找相关的数据库连接mysql类哦。

本文章来给各位同学介绍一个关于PHP重启路由器以更换IP地址程序,如果你对此有兴趣不防进入参考哦。
在采集大批量数据时常常会触发对方服务器的“自我保护”,请求过于频繁就限制访问。这时需要停留很长一段时间(十几分钟到几十分钟不等)才能恢复访问,这样采集数据的速度就受到非常大的限制。
解决方法有两个:
1 通过图片识别绕过验证码机制,告诉服务器:我不是蜘蛛,我是人。不信你瞧,我能看懂验证码。
2 更换IP,告诉服务器:我不是张三,我是李四。不信你瞧,我的IP地址和张三的不一样。
第一个方法难度稍高一点而且不靠谱,等哪天对方服务器升级了验证码了,这边也得跟进,麻烦多;而ISP(电信、联通、移动)那儿有很多IP,每次联网都会分配一个新的IP,因此方法二比较好。
以我的TP-LINK路由器为例,找到“网络参数”>“WAN口设置”,可以看到“自动连接”设置和“断线”按钮。每次点击“断线”按钮,就向ISP重新拨号,此时就换了一个IP。但大批量数据的采集需要的时间比较长,不可能总有人在旁边守着,最好能在PHP代码中,一旦发现被限制了就重启一次,这就回到本文的主题了:《通过PHP函数重启路由器以更换IP》
打开chrome浏览器的调试模式,然后点击“断线”按钮,看“Network”网络请求,可以看到实际执行的地址是:“http://192.168.0.1/userRpm/PPPoECfgRpm.htm?wantype=2&acc=65541234&psw=Hello123World&VnetPap=0&linktype=2&Disconnect=%B6%CF+%CF%DF”
然后模拟请求这个地址,经测试确实可以更换IP地址(通过http://api.akcms.com/myip.php可以看到当前IP)。接下来的就简单了:就用PHP使用Curl组件来实现这个请求的过程,我封装了一个函数resetip,具体代码如下:
 代码如下 复制代码
<?php
//本脚本测试重启路由器的WLAN连接
resetip();

function resetip() {
	$username = 'admin';
	$password = '123456';
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.1/userRpm/PPPoECfgRpm.htm?wantype=2&acc=65541234&psw=Hello123World&VnetPap=0&linktype=2&Disconnect=%B6%CF+%CF%DF');
	curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_exec($ch);
	curl_close($ch);
}
?>
高亮处需要根据自己的情况修改,路由器地址有的是192.168.1.1,重启地址也各有不同,但大同小异,自己改改。
本文章来给大家介绍一个Drupal 通过cURL Post方式发送一个文件实现,如果你正在使用Drupal cms不防进入参考一下哦。

众所周知PHP的cURL扩展可以用来模拟表单提交。在Drupal中有drupal_http_request函数来执行一个HTTP请求,它可以通过POST方式来发送一个文件,但是使用起来没有cURL那么方便。 这里我们主要讲解如何在Drupal中Post一个文件到远程的服务器地址。

网页Form表单

 代码如下 复制代码
<form enctype=”multipart/form-data” method=”POST” url=”http://blog.lixiphp.com/demo/http_request/post.php”>
<input name=”username” type=”text” value=”" />
<input name=”password” type=”password” value=”" />
<input type=”checkbox” name=”rememberme” id=”" />
<input name=”avatar” type=”file” />
</form>

上面表单包含了演示文本框、密码、复选框和文件形式的提交。

Drupal cURL模拟表单提交POST

 代码如下 复制代码

<?php
$url = ‘http://blog.lixiphp.com/demo/http_request/post.php’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
‘username’ => ‘lixiphp’,
‘password’ => ’123456′,
‘rememberme’ => ’1′,
‘avatar’=> ‘@’.$filename,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);

$response

的值为网页Form表单提交后输出的HTML。

我们要生成二维码都需要借助一些类库来实现了,下面我介绍利用PHP QR Code生成二维码吧,生成方法很简单,下面我来介绍一下。

利用php类库PHP QR Code来实现,不需要装额外的php扩展
首先下载类库包,有时候地址打不开
地址:http://phpqrcode.sourceforge.net/
下载:http://sourceforge.net/projects/phpqrcode/

使用时一般引入phpqrcode.php文件即可

具体使用方法举例

直接浏览器输出:

 代码如下 复制代码

<?php
    include "phpqrcode/phpqrcode.php";
    $value="http://www.111cn.net";
    $errorCorrectionLevel = "L";
    $matrixPointSize = "4";
    QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
    exit;
?>

图片文件输出

 代码如下 复制代码

<?php
   
//文件输出
    include('phpqrcode/phpqrcode.php');
   
// 二维码数据
    $data = 'http://www.111cn.net';
   
// 生成的文件名
    $filename = '1111.png';
   
// 纠错级别:L、M、Q、H
    $errorCorrectionLevel = 'L';
   
// 点的大小:1到10
    $matrixPointSize = 4;
    QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
?>

生成中间带logo的二维码

 代码如下 复制代码

<?php
   
//生成中间带logo的二维码
    include('phpqrcode/phpqrcode.php');
    $value='http://www.111cn.net';
    $errorCorrectionLevel = 'L';
    $matrixPointSize = 10;
    QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);
    echo "QR code generated"."<br />";
    $logo = 'bdlogo.gif';
    $QR = 'xiangyang.png';
 
    if($logo !== FALSE)
    {
 
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);
        $logo_width = imagesx($logo);
        $logo_height = imagesy($logo);
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    }
    imagepng($QR,'xiangyanglog.png');
?>

在php中删除数组元素与增加数组元素的方法有很多种,下面小编来给各位同学总结一些实用的与常用的数组元素增加删除实例。

增加数据元素有函数,array_push(),array_unshift()函数

一、在数组的末尾添加元素

1.array_push

使用方法

 代码如下 复制代码

<?php
    $stack = array("orange", "banana");
    array_push($stack, "apple", "raspberry");
    print_r($stack);
?>
输出:
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

2.$arr[]

使用方法

<?php

    $arr = array("orange", "banana");
    $arr[]='apple';
    print_r($arr);
?>

这两种的效果是一样的

注意:如果用 array_push() 来给数组增加一个单元,还不如用 $array[] = ,因为这样没有调用函数的额外负担。

二、在数组开头插入元素

1.array_unshift

使用方法

 代码如下 复制代码

<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>

输出

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

删除数组元素unset,或直接设置空


如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊

 代码如下 复制代码


<?php 
$arr = array('a','b','c','d'); 
unset($arr[1]); 
print_r($arr); 
?> 
 
print_r($arr)

之后,结果却不是那样的,最终结果是 Array ( [0] => a [2] => c [3] => d )

 

那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice():

 代码如下 复制代码

<?php 
$arr = array('a','b','c','d'); 
array_splice($arr,1,1); 
print_r($arr);
?>

print_r($arr)之后,结果是Array ( [0] => a [1] => c [2] => d )

删除数组指定元素

如array_slice() 函数在数组中根据条件取出一段值,并返回.

array_slice(array,offset,length,preserve)

array:数组

offset: 规定取出元素的开始位置。如果是正数,则从前往后开始取,如果是负值,从后向前取 offset 绝对值。

 

 代码如下 复制代码

<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>

输出

Array ( [0] => Cat [1] => Horse )

还有array_shift() 函数删除数组中的第一个元素,并返回被删除元素的值.

相对的array_pop() 函数删除数组中的最后一个元素.

几个函数用下来觉得array_search()比较实用

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false

 代码如下 复制代码


$array = array('1', '2', '3', '4', '5');

$del_value = 3;
unset($array[array_search($del_value , $array)]);//利用unset删除这个元素

print_r($array);

输出

array('1', '2', '4', '5');

[!--infotagslink--]

相关文章

  • 浅析Promise的介绍及基本用法

    Promise是异步编程的一种解决方案,在ES6中Promise被列为了正式规范,统一了用法,原生提供了Promise对象。接下来通过本文给大家介绍Promise的介绍及基本用法,感兴趣的朋友一起看看吧...2021-10-21
  • C#实现图片放大功能的按照像素放大图像方法

    这篇文章主要介绍了C#实现图片放大功能的按照像素放大图像方法,功能非常实用,需要的朋友可以参考下...2020-06-25
  • python中翻译功能translate模块实现方法

    在本篇文章中小编给各位整理了一篇关于python中翻译功能translate模块实现方法,有需要的朋友们可以参考下。...2020-12-18
  • 微信小程序实现导航功能的操作步骤

    这篇文章主要给大家介绍了关于微信小程序实现导航功能的操作步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • PHP中print_r、var_export、var_dump用法介绍

    文章详细的介绍了关于PHP中print_r、var_export、var_dump区别比较以及这几个在php不同的应用中的用法,有需要的朋友可以参考一下 可以看出print_r跟var_export都...2016-11-25
  • Framewrok7 视图介绍(views、view)使用介绍

    下面我们来看一篇关于Framewrok7 视图介绍(views、view)使用介绍吧,希望这篇文章能够帮助到各位朋友。 一、Views 与View的介绍 1,Views (<div class="views">) (1)Vi...2016-10-02
  • AngularJS 让人爱不释手的八种功能

    AngularJS 让人爱不释手的八种功能,想知道AngularJS哪八种功能让人喜欢就快点看下本文吧...2016-03-28
  • EMUI11上手体验 新颜值/新功能/新体验

    EMUI11值得升级吗?好不好用?下面小编带来EMUI11上手体验,一起来看看手机鸿蒙OS的提前预演...2020-12-08
  • phpMyAdmin 高级功能设置的方法图解

    phpmyadmin还有高级功能可能大部份站长不知道吧,今天本文章就来给大家介绍phpMyAdmin 高级功能设置的方法图解,希望文章对大家会有所帮助。 phpMyAdmin 安装后,默认...2016-11-25
  • 小爱同学5.0新增了哪些机型 小爱同学5.0新功能介绍

    小爱同学5.0即将发布,据已知报道小爱同学5.0将新增机型,跟着小编一起来看看吧,顺便了解下即将都有哪些新功能面市吧...2020-12-08
  • Monolog PHP日志类库使用详解介绍

    PHP日志类库在低版本中我们都没有看到了但在高版本的php中就有了,下面我们来看一篇关于PHP日志类库使用详解介绍吧. Monolog遵循PSR3的接口规范,可以很轻易的替换...2016-11-25
  • 很全面的JavaScript常用功能汇总集合

    这篇文章主要为大家分享了一份很全面的JavaScript常用功能汇总集合,一些常用的额JS 对象、基本数据结构、功能函数等,感兴趣的小伙伴们可以参考一下...2016-01-24
  • php获取当前url地址的方法介绍

    这篇文章介绍了php获取当前url地址的方法小结,有兴趣的同学可以参考一下 本文实例讲述了php获取当前url地址的方法。分享给大家供大家参考,具体如下: js 获取: ...2017-01-22
  • 不错的mod_perl编程的简单应用实例介绍

    介绍性指南 mod_perl 是个庞大而复杂的工具,它内建了许多模块帮助你方便地构建动态网站。这篇指南的目的是帮助你构建一个良好的 mod_perl 模块,并从中理解 mod_perl 的实现...2020-06-29
  • PHP-GTK 介绍及其应用

    1. PHP-GTK介绍 1.1 PHP-GTK PHP-GTK是PHP的延伸模组,它可以让程式设计师写出在客户端执行的、且独立的GUI的程式。这个模组不允许在浏览器上显视GTK+的程式,它一开始就...2016-11-25
  • Night Shift是什么意思 Night Shift有什么功能及作用?

    Night Shift是IOS9.3正式版系统新增加的功能之一,很多伙伴们都不清楚Night Shift是什么意思?以及Night Shift有什么用途?对此,本文小编就为大家详细介绍Night Shift的含义及作用...2016-07-04
  • PHP 获取文件目录权限函数fileperms介绍

    在php中要获取或目录权限我们可使用fileperms函数来获取,fileperms() 函数返回文件或目录的权限,。若成功,则返回文件的访问权限。若失败,则返回 false。 例子 1 ...2016-11-25
  • php设置时区方法介绍

    php默认时区是欧美国家的所以与我们中国时区相差了整整8小时哦,下面我来给各位介绍php设置时区方法,有需要了解的朋友可进入参考。 在 php.ini 中,默认是 date.timez...2016-11-25
  • 使用php的编码功能-实例调用(3)

    <?php include_once("mime.inc"); $mm = new MIME(); $to = "customer@263.net"; $subject = $mm->encode("商城","gb2312"); // 编码 $msg = "注册会员成功<br>"; $m...2016-11-25
  • OpenCart网站迁移步骤详细介绍

    OpenCart是国外著名的开源电子商务系统,由英国人Daniel一人独立开发,其社区非常活跃,由各国网友翻译出来的语言包已经达到18种,其中包括中文,俄文,法文,西班牙文,德文等等,下面...2016-10-10