apache,cgi,libcurl和json的初次使用

libcurl和json的初次使用

apache安装

sudo apt-get install apache2
静态网页位置:/var/www/html
apache可执行程序CGI存放位置:/usr/lib/cgi-bin/

cgi模块支持启用

cd /etc/apache2/mods-enable
sudo ln -s ../mods-available/cgid.conf
sudo ln -s ../mods-available/cgid.load
sudo ln -s ../mods-available/cgi.load
sudo /etc/init.d/apache2 restart

libcurl库的安装

sudo apt-get install curl
安装curl命令,该命令将curl安装/usr/bin
sudo apt-get install libcurl4-openssl-dev
来安装libcurl,libcurl的安装路径
头文件:/usr/include/curl
库目录:/usr/lib

json

用git克隆获取源代码
命令:git clone https://github.com/json-c/json-c.git

这里写图片描述

示例代码

代码说明:(理解为客户端,必须放在/usr/lib/cgi-bin/ 目录下编译)
1.将用户名和密码打包成JSON格式
2.用libcurl以post的方式将数据提交给服务器,并访问a.out (CGI程序),此程序验证用户名和密码是否正确。
编译:gcc *.c cJSON.c -lcurl -lm (cJSON.c,cJSON.h库拷贝到当前目录)

#include <stdlib.h>
#include <curl/curl.h>
#include "cJSON.h"

int main()
{
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "username", "aa");
    cJSON_AddStringToObject(root, "password", "bb");
    char* buf = cJSON_Print(root);

    CURL* curl = curl_easy_init();

    // {username:"aa", password:"bb"}

    // 默认是GET方式,如果想改成POST方式
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf); // username=aa&password=bb
    curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/cgi-bin/a.out");

    // POST方式
    curl_easy_perform(curl);
    cJSON_Delete(root);
    free(buf);
}

代码说明:
1.此为上述代码的a.out(CGI程序),将发送过来的JSON打包格式buf读取出来
2.buf读取出来后,再通过JSON解包,就可以获取用户名和密码进行验证。
编译:gcc *.c cJSON.c -lm (cJSON.c,cJSON.h库拷贝到当前目录)

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

extern char** environ;

int main()
{
    // 规定
    printf("content-type: text/html\n\n");

    int content_len = atoi( getenv("CONTENT_LENGTH") );

    char* buf = malloc(content_len + 1);
    buf[content_len] = 0;

    // 读的是客户端发送过来的POST数据
    fread(buf, content_len, 1, stdin);

    cJSON* root = cJSON_Parse(buf);
    cJSON* user = cJSON_GetObjectItem(root, "username");
    cJSON* pass = cJSON_GetObjectItem(root, "password");

    if(strcmp(user->valuestring, "aa") == 0
            && strcmp(pass->valuestring, "bb") == 0)
    {
        printf("login ok\n");
    }
    else
    {
        printf("login error\n");
    }
}
# LAMP Auto Install Script for CentOS 7 #centos7下编写自动化脚本搭建lamp架构,下载目录为/opt/lamp1/,需求版本:apache-2.4.63、php-7.4.33、mysql-8.0.41-1.el9.x86_64.rpm-bundle.tar,php不用单独启动,apache联动即可 #要求: #目前线上apache使用的模块为(core_module, authn_file_module, authn_default_module, authz_host_module, authz_groupfile_module, authz_user_module, authz_default_module, auth_basic_module, include_module, filter_module, log_config_module, env_module, setenvif_module, version_module, mpm_prefork_module, http_module, mime_module, status_module, autoindex_module, asis_module, cgi_module, negotiation_module, dir_module, actions_module, userdir_module, alias_module, rewrite_module, so_module, ssl_module (shared), xsendfile_module (shared), php7_module (shared)) #目前线上php使用的模块为(bcmath,bz2,Core,ctype,curl,date,dom,ereg,fileinfo,filter,gd,gettext,hash,iconv,json,libxml,mbstring,mcrypt,mhash,mysql,mysqli,mysqlnd,openssl,pcntl,pcre,PDO,pdo_mysql,pdo_sqlite,Phar,posix,Reflection,session,shmop,SimpleXML,soap,sockets,SPL,sqlite3,standard,sysvmsg,sysvsem,sysvshm,tokenizer,xml,xmlreader,xmlwriter,zip,zlib) #保留以上模块并解决依赖关系,此外apache、php的模块全部启用并加载 #安装apachephp的依赖模块apr-1.7.5、apr-util-1.6.3、ibiconv-1.15、libmcrypt-2.5.8、mhash-0.9.9.9、mcrypt-2.6.8、libmcrypt-devel并做好环境变量软连接 #验证apachephp联动访问,MySQL登录初始化、密码设置为1hblsqt2zhlmcl#@!
03-11
#centos7下编写自动化脚本搭建lamp架构,下载目录为/opt/lamp1/,需求版本:apache-2.4.63、php-7.4.33、mysql-8.0.41-1.el9.x86_64.rpm-bundle.tar,php不用单独启动,apache联动即可 #要求: #安装apachephp的依赖模块apr-1.7.5、apr-util-1.6.3、ibiconv-1.15、libmcrypt-2.5.8、mhash-0.9.9.9、mcrypt-2.6.8、libmcrypt-devel并做好环境变量软连接 #apache开启模块如下模块并解决依赖问题(core_module, authn_file_module, authn_default_module, authz_host_module, authz_groupfile_module, authz_user_module, authz_default_module, auth_basic_module, include_module, filter_module, log_config_module, env_module, setenvif_module, version_module, mpm_prefork_module, http_module, mime_module, status_module, autoindex_module, asis_module, cgi_module, negotiation_module, dir_module, actions_module, userdir_module, alias_module, rewrite_module, so_module, ssl_module (shared), xsendfile_module (shared), php7_module (shared)) #php开启模块如下模块并解决依赖问题(bcmath,bz2,Core,ctype,curl,date,dom,ereg,fileinfo,filter,gd,gettext,hash,iconv,json,libxml,mbstring,mcrypt,mhash,mysql,mysqli,mysqlnd,openssl,pcntl,pcre,PDO,pdo_mysql,pdo_sqlite,Phar,posix,Reflection,session,shmop,SimpleXML,soap,sockets,SPL,sqlite3,standard,sysvmsg,sysvsem,sysvshm,tokenizer,xml,xmlreader,xmlwriter,zip,zlib) #保留以上模块并解决依赖关系,此外apache、php的模块全部启用并加载 #验证apachephp联动访问,MySQL登录初始化、密码设置为1hblsqt2zhlmcl#@!
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值