Press "Enter" to skip to content

标签: c

Nginx HTTP Post Method: 405 Method not allowed解决方法

最近维护一台RedHat 5.4 X64系统,环境是Nginx,跑着一个论坛,需要向HTML页面提交POST数据,结果都被拦截下来了,显示错误:“nginx 405 Not Allowed”,是乎没有很好的解决办法,唯一能做的就是重新编译Nginx源码和编辑conf文件。
需要修改Nginx中的C源码文件位于 /nginx源码目录/src/http/modules/ngx_http_static_module.c ,找到如下代码:

    if (r->method == NGX_HTTP_POST) {
        return NGX_HTTP_NOT_ALLOWED;
    }


注释掉如下:

/*if (r->method == NGX_HTTP_POST) {
        return NGX_HTTP_NOT_ALLOWED;
    }
*/


然后再重新编译 make
复制/nginx源码目录/ objs 目录下的 nginx至安装的Nginx目录下,重启Nginx生效。

对于Nginx,可以修改nginc.conf配置文件,改变“405错误”为“200 ok”,并配置location来解决,方法如下:

server
{
    listen       80;
    server_name  www.kiccleaf.com;
    index index.html index.htm index.php;
    root  /data/kiccleaf;  

    if ($host != 'www.kiccleaf.com' ) {
         rewrite ^/(.*)$ http://www.kiccleaf.com/$1 permanent;
    }
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    #添加以下405代码
    error_page   405 =200 @405;
    location @405
    {
        root  /data/kiccleaf;
    }
}


也可以简单的编写成

server
{
    listen       80;
    server_name  www.kiccleaf.com;
    index index.html index.htm index.php;
    root  /data/kiccleaf;  

    if ($host != 'www.kiccleaf.com' ) {
         rewrite ^/(.*)$ http://www.kiccleaf.com/$1 permanent;
    }
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    #添加以下405代码
    error_page 405 =200 $uri;
}
Leave a Comment

Linux下c读取MysqL中文乱码解决方案

在编写接口API时,发现中文字utf8输入的在linux下采用c读取显示为”??”问号,这是由于编码造成的。很简单的两个地方做修改就搞定。
1.先找到mysql的my.cnf配置文件/etc/my.cnf编辑添加
[mysqld]
default-character-set=utf8

2.在程序中添加 mysql_set_character_set(&db,”utf8″); 语句
例子:kiccleaf.c 文件

#include <stdio.h>
//这里需要找到相应地址不知道可以用“ find / -name mysql.h ”查询
#include "/usr/include/mysql/mysql.h"
int main()
{
    MYSQL db;/*connector*/
    MYSQL_RES* result;/*result buffer*/
    MYSQL_ROW row;/*one row of the result*/
    int i;
    if(mysql_init(&db) ==NULL)
    {
      fprintf(stderr,"Fail to initialize the db.\n");
      return -1;
    }
    if(!mysql_real_connect(&db,"localhost","root","password","kiccleaf",3306,NULL,0))
    {
      fprintf(stderr,"Fail to connect to the server");
      return -1;
    }
    //添加字符集防止乱码
    mysql_set_character_set(&db,"utf8");
    if(mysql_query(&db,"SELECT * FROM user") != 0)
    {
      fprintf(stderr,"Fail to query the db for information.\n");
      return -1;
    }
    if ((result = mysql_store_result(&db)) == NULL)
    {
      fprintf(stderr,"Fail to get the result.\n");
      return -1;
    }

    while((row=mysql_fetch_row(result)) != NULL)/*fetching each row*/
    {
      puts("================================================");
      printf("id: %s\n",row[0]);
      printf("name: %s\n",row[1]);
      printf("pwd: %s\n",row[2]);
      printf("flag: %s\n",row[3]);
      puts("================================================");
    }

    mysql_free_result(result);
    mysql_close(&db);
    return 0;
}

编译时也会出现问题,可以添加参数 “-lz /usr/lib/mysql/libmysqlclient.so.15.0.0”

gcc -o kiccleaf -g kiccleaf.c -lz /usr/lib/mysql/libmysqlclient.so.15.0.0

原来乱码的情况

[root@kiccleaf c]# ./kiccleaf
================================================
id: 0000000001
name: ???
pwd: 123456
flag: 0
================================================
================================================
id: 0000000002
name: ???
pwd: 654123
flag: 0
================================================
================================================
id: 0000000003
name: ???
pwd: 789456
flag: 0
================================================
================================================
id: 0000000004
name: ???
pwd: 456321
flag: 0
================================================

添加字符集后输出结果:

[root@kiccleaf c]# ./kiccleaf
================================================
id: 0000000001
name: 测试1
pwd: 123456
flag: 0
================================================
================================================
id: 0000000002
name: 测试2
pwd: 654123
flag: 0
================================================
================================================
id: 0000000003
name: 测试3
pwd: 789456
flag: 0
================================================
================================================
id: 0000000004
name: 测试4
pwd: 456321
flag: 0
================================================
Leave a Comment

C语言获取汉字拼音首字母

在网络上找了一份C语言获取汉字拼音首字母的源程序,先保存一下。这里用了glib里面的一个函数g_convert。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
  
gchar getpychar(guchar uword0 , guchar uword1)
{
    gchar pychar;
  
    int i1 = (short)(uword0 - '\0');
    int i2 = (short)(uword1 - '\0');
  
    int tmp = i1 * 256 + i2;
  
    if(tmp >= 45217 && tmp <= 45252) pychar = 'A'; 
    else if(tmp >= 45253 && tmp <= 45760) pychar = 'B'; 
    else if(tmp >= 45761 && tmp <= 46317) pychar = 'C'; 
    else if(tmp >= 46318 && tmp <= 46825) pychar = 'D'; 
    else if(tmp >= 46826 && tmp <= 47009) pychar = 'E'; 
    else if(tmp >= 47010 && tmp <= 47296) pychar = 'F'; 
    else if(tmp >= 47297 && tmp <= 47613) pychar = 'G'; 
    else if(tmp >= 47614 && tmp <= 48118) pychar = 'H'; 
    else if(tmp >= 48119 && tmp <= 49061) pychar = 'J'; 
    else if(tmp >= 49062 && tmp <= 49323) pychar = 'K'; 
    else if(tmp >= 49324 && tmp <= 49895) pychar = 'L'; 
    else if(tmp >= 49896 && tmp <= 50370) pychar = 'M'; 
    else if(tmp >= 50371 && tmp <= 50613) pychar = 'N'; 
    else if(tmp >= 50614 && tmp <= 50621) pychar = 'O'; 
    else if(tmp >= 50622 && tmp <= 50905) pychar = 'P'; 
    else if(tmp >= 50906 && tmp <= 51386) pychar = 'Q'; 
    else if(tmp >= 51387 && tmp <= 51445) pychar = 'R'; 
    else if(tmp >= 51446 && tmp <= 52217) pychar = 'S'; 
    else if(tmp >= 52218 && tmp <= 52697) pychar = 'T'; 
    else if(tmp >= 52698 && tmp <= 52979) pychar = 'W'; 
    else if(tmp >= 52980 && tmp <= 53640) pychar = 'X'; 
    else if(tmp >= 53689 && tmp <= 54480) pychar = 'Y'; 
    else if(tmp >= 54481 && tmp <= 55289) pychar = 'Z'; 
    else pychar = ' ';
  
    return pychar;
}
  
gchar *getpystring(const gchar *in)
{
    gsize inlen , olen , i , j = 0;
    gchar *gword = g_convert(in , strlen(in)
            , "gb2312" , "utf8" , &inlen , &olen , NULL);
  
    guchar *uword = (guchar*)gword;
    gchar *out = (gchar*)malloc(olen);
  
    memset(out , 0 , olen);
  
    for(i = 0 ; i < olen ; i++){
        if(uword[i] >= 0xa1){
            if(uword[i] != 0xa3){
                out[j++] = getpychar(uword[i] , uword[i + 1]);
                i ++;
            }
        }else{
            out[j++] = (gchar)uword[i];
        }
    }
  
    return out;
  
}
  
int main(int argc , char **argv)
{
    printf("%s\n" , getpystring("linux是一个出色的操作系统"));
    return 0;
  
}

程序输出:linuxSYGCSDCZXT

Leave a Comment

一个校验身份证号码合法性的C程序

一个校验身份证号码合法性的C程序,收藏一下。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
 
int IsDigitBuf(char *sBuf, int nLen)
{
    int    i;
 
    if (nLen == 0) return 1;
    if (nLen > strlen(sBuf)) nLen = strlen(sBuf);
 
    for (i = 0; i < nLen; i++)
        if (!isdigit(sBuf[i])) return 0;
 
    return 1;
}
 
int checkdate(int iYear, int iMonth, int iDay)
{
    if (iYear < 0 || iYear > 9999)
    return -1;
    switch (iMonth)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if (iDay <= 0 || iDay > 31)
        {
            return -3;
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if (iDay <= 0 || iDay > 30)
        {
            return -3;
        }
        break;
    case 2:
        if ((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0)
        {
            if (iDay <= 0 || iDay > 29)
            {
                return -3;
            }
        }
        else
        {
            if (iDay <= 0 || iDay > 28)
            {
                return -3;
            }
        }
        break;
    default:
        return -2;
    }
    return 0;
}
 
int CheckStrDate(char *sDate)
{
    int iRet;
 
    char sYear[5];
    char sMonth[3];
    char sDay[3];
 
    memset(sYear,  0, sizeof(sYear));
    memset(sMonth, 0, sizeof(sMonth));
    memset(sDay,   0, sizeof(sDay));
 
    if (strlen(sDate) != 8 )
    {
        return -1;
    }
 
    memcpy(sYear,  sDate, 4);
    memcpy(sMonth, sDate+4, 2);
    memcpy(sDay,   sDate+6, 2);
 
    iRet = checkdate(atoi(sYear), atoi(sMonth), atoi(sDay));
    if (iRet != 0)
    {
        return -1;
    }
 
    return 0;
}
 
 
int main(int argc, char *argv[])
{
    int    i;
    int    iRet;
    int    iWeight[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    char   cCheck[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    char   sIdCardNo[20];
    char   sDate[8 + 1];
    int    iDate;
    int    Sum = 0;
 
    if (argc < 2) {
        printf("FAIL\n");
        exit(1);
    }
 
    memset(sIdCardNo, 0, 20);
    memcpy(sIdCardNo, argv[1], 18);
    if ((strlen(sIdCardNo) == 15) && ((iRet = IsDigitBuf(sIdCardNo, 15)) > 0)) {
        printf("OK\n");
        exit(0);
    } else if (strlen(sIdCardNo) != 18) {
        printf("FAIL\n");
        exit(1);
    }
 
    /* 身份证7-14位是否有效 */
    memset(sDate, 0, sizeof(sDate));
    memcpy(sDate, sIdCardNo + 6, 4);
    iDate = atoi(sDate);
    if (iDate < 1900 || iDate > 2020) {
        printf("FAIL\n");
        exit(1);
    }
    memset(sDate, 0, sizeof(sDate));
    memcpy(sDate, sIdCardNo + 6, 8);
    iRet = CheckStrDate(sDate);
    if (iRet < 0) {
        printf("FAIL\n");
        exit(1);
    }
 
    /* 身份证18位校验位是否有效 */
    for (i = 0; i < 17; i ++) {
        memset(sDate, 0, sizeof(sDate));
        sDate[0] = sIdCardNo[i];
        iDate = atoi(sDate);
        Sum += iWeight[i] * iDate;
    }
    Sum %= 11;
    if ('x' == sIdCardNo[17]) {
        sIdCardNo[17] = 'X';
    }
    if (cCheck[Sum] != sIdCardNo[17]) {
        printf("FAIL\n");
        exit(1);
    }
 
    printf("OK\n");
    exit(0);
}
Leave a Comment

c 字符串小写转大写

常用的C小写字母转大写程序

#include <stdio.h>
int main(void)
    {
          int i=0;
          char string[100];
          strcpy(string,"abcdefghijklmnopqrstuvwxyz");
     while (string[i]!='\0'){ //将小写转化成大写
                  if (islower(string[i]))
                      string[i]=toupper(string[i]);
           i++;
       }
       printf("%s\n",string);
       return 0;
    }  

输出:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Leave a Comment

C语言写文件

C语言写文件操作,先记录一下。

#include<stdio.h>
#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}
#define nmemb 3
struct test
{
    char name[20];
    int size;
}
s[nmemb];
main()
{
    FILE * stream;
    set_s(0,"Linux!");
    set_s(1,"FreeBSD!");
    set_s(2,"Windows2000.");
    stream=fopen("/tmp/fwrite","w");
    fwrite(s,sizeof(struct test),nmemb,stream);
    fclose(stream);
}
Leave a Comment

编写一个改变文件读写属性的C程序

编写一个改变文件读写属性的C程序

[root@kiccleaf leaf]# ll

-rwxr-xr-x  1 root root      5277 Aug  9 19:47 mysql.sh
#采用VIM编辑c源程序
[root@kiccleaf leaf]# vim chmd.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h> 
 
int main()
{
  if(chmod("./mysql.sh",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)==0)
  {
    printf("设置成功!\n");
  }
  else
  {
    printf("设置失败!文件不存在!\n");
  }
  return 0;
}
#采用gcc编译生成chmd可执行文件
[root@kiccleaf leaf]# gcc -g -Wall -o chmd chmd.c

[root@kiccleaf leaf]# ./chmd
[root@kiccleaf leaf]# ll

-rw-r--r--  1 root root      5277 Aug  9 19:47 mysql.sh

文件显示“-rw-r–r–
r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。

chmod()改变文档存取权限的函数

原形:int chmod(const char *path,int amode)

功能:根据amode的值,配置由path所指文档的存取权限。

amode的值     存取权限

S_IRUSR 用户可以读
S_IWUSR 用户可以写
S_IXUSR 用户可以执行
S_IRWXU 用户可以读写执行
———————————–
S_IRGRP 组可以读
S_IWGRP 组可以写
S_IXGRP 组可以执行
S_IRWXG 组可以读写执行
———————————–
S_IROTH 其他人可以读
S_IWOTH 其他人可以写
S_IXOTH 其他人可以执行
S_IRWXO 其他人可以读写执行
———————————–
S_ISUID 设置用户执行ID
S_ISGID 设置组的执行ID

———————————–
返回值:0(成功);-1(失败)

Leave a Comment