• CentOS查看内存信息和频率

    内存条数

    Linux查看内存的插槽数,已经使用多少插槽。每条内存多大,已使用内存多大可以使用以下命令

    [root@test01 ~]# dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range
            Size: 4096 MB
    

    上面的结果中,只看到一个4096 MB表示我只插了一条内存,大小是4G

    [root@test02 ~]# dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range
            Size: 4096 MB
            Size: 4096 MB
    

    ...

    READ ALL

  • PHP中memcached()函数使用

    PHP中提供两个函数可以访问memcached,一个是旧的memcache()和一个新的memcached(),两个函数都差不多,后者据说比前者有一些优化

    环境

    在使用memcached()函数之前,需要确认一下有没有配好环境

    phpinfo.php

    <?php
    // 琼台博客 www.qttc.net
    echo phpinfo();
    ?>
    

    假如你能在页面中看见memcached support关键字就表示你可以使用了,如果没看见那需要先把环境配置好

    使用

    使用特别简单,它是一个类,直接实例化既可以使用

    <?php
    // 琼台博客 www.qttc.net
    $mem_var = new Memcached();
    $mem_var->addServer("127.0.0.1", 11211);
    $response = $mem_var->get("qttc");
    if ($response) {
      echo $response;
    } else {
      echo "Hello";
      $mem_var->set("qttc", "Hello") or die(" Keys Couldn't be Created : qttc Not Found :'( ");
    }
    

    ...

    READ ALL

  • PHP中strtotime()函数使用

    这个函数的作用是传入一定文本格式的参数,返回一个时间戳,可以使用它来获取一个时间戳,比如获取前一天,前一个月,前一年等等。

    这个函数的使用场景非常多,比如查询一个月内的数据,查询一天内的数据。

    语法

    int strtotime ( string $time [, int $now = time() ] )

    参数

    例子

    // 琼台博客 www.qttc.net
    
    echo strtotime("now"), "\n";
    echo strtotime("10 September 2000"), "\n";
    echo strtotime("+1 day"), "\n";
    echo strtotime("+1 week"), "\n";
    echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
    echo strtotime("next Thursday"), "\n";
    echo strtotime("last Monday"), "\n";
    

    ...

    READ ALL

  • PHP中file_get_contents函数不能使用的解决方法

    有些主机服务商把PHP的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl

    下面是file_get_contents和curl两个函数同样功能的不同写法

    file_contents

    // 琼台博客 www.qttc.net
    
    $file_contents = file_get_contents("https://www.qttc.net/");
    echo $file_contents;
    

    换成curl函数的使用示例

    ...

    READ ALL

  • HTML5新特性

    WebSocket

    HTML5开始支持WebSocket,在这之前只能通过长连接或者flash控件去做通讯,WebSocket使用的业务场景最多应该是聊天室了。

    格式

    var Socket = new WebSocket(url, [protocal] );

    // 琼台博客 www.qttc.net
    
    if ("WebSocket" in window)
    {
      alert("WebSocket is supported by your Browser!");
      
      // Let us open a web socket
      var ws = new WebSocket("ws://localhost:9998/echo");
    
      ws.onopen = function()
      {
        // Web Socket is connected, send data using send()
        ws.send("Message to send");
        alert("Message is sent...");
      };
    
      ws.onmessage = function (evt) 
      { 
        var received_msg = evt.data;
        alert("Message is received...");
      };
    
      ws.onclose = function()
      { 
        // websocket is closed.
        alert("Connection is closed..."); 
      };
    
      window.onbeforeunload = function(event) {
        socket.close();
      };
    }
    
    else
    {
      // The browser doesn't support WebSocket
      alert("WebSocket NOT supported by your Browser!");
    }
    

    ...

    READ ALL

  • MySQL常用日期函数

    DAYOFWEEK(date)

    返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)

    mysql> select DAYOFWEEK('2012-03-20');
    -> 3
    

    WEEKDAY(date)

    返回日期date是星期几(0=星期一,1=星期二,……6= 星期天)。

    mysql> select WEEKDAY("2012-03-20 22:23:00");
    -> 1
    
    mysql> select WEEKDAY('2012-03-20');
    -> 1
    

    DAYOFMONTH(date)

    返回date是一月中的第几日(在1到31范围内)

    ...

    READ ALL

  • PHP中explode()与implode()函数的使用

    explode()

    explode()是字符串分割函数

    格式

    array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

    返回一个全是字符串的数组

    // 琼台博客 www.qttc.net
    
    $str = "Hello world. here is qttc.";
    print_r (explode(" ",$str));
    

    implode()

    implode()是连接字符串函数

    格式

    string implode ( string $glue , array $pieces )

    ...

    READ ALL

  • PHP使用flush实现长连接

    每次我们访问PHP脚本的时候,都是当所有的PHP脚本执行完成后,我们才得到返回结果。如果我们需要一个脚本持续的运行,那么我们就要通过php长连接的方式,来达到运行目的。

    每个PHP脚本都限制了执行时间,所以我们需要通过set_time_limit来设置一个脚本的执行时间为无限长。然后使用flush()ob_flush()来清除服务器缓冲区,随时输出脚本的返回值。

    Code

    // 琼台博客 www.qttc.net
    
    header("Content-Type: text/plain");
    set_time_limit(0);
     
    $infoString = "Hello World" . "\n";
    while( isset($infoString) )
    {
      echo $infoString;
      flush();
      ob_flush();
      sleep(5);
    }
    

    ...

    READ ALL

  • PHP替换字符串函数strtr()

    strtr(string,from,to)

    逐个字符开始替换,以from跟to中长度较较短的一个为准,例如:

    // 琼台博客 www.qttc.net
    
    echo strtr("aidenliu", "ai", "b");
    // Output: bidenliu
    
    // 琼台博客 www.qttc.net
    
    echo strtr("aidenliu", "a", "bc");
    // Output: bidenliu
    

    此函数是大小写敏感的,具如果发生多次替换,每一次替换的蓝本都是最原始的那个字符串,而不是在前一次替换的基础上替换,如

    ...

    READ ALL

  • jQuery中使用Ajax

    jQuery封好了许多直接可以使用的Ajax请求函数

    GET请求

    $.get()这个方法可以发起一个GET请求

    // 琼台博客 www.qttc.net
    
    $.get('/api_by_get', ($res) => {
      console.log($res);
    })
    

    在回调中获取响应结果

    POST请求

    $.post()这个方法可以发起一个POST请求

    // 琼台博客 www.qttc.net
    
    $.post'/api_by_post', "Hello world", ($res) => {
      console.log($res);
    })
    

    ...

    READ ALL