How is Seesion used on the C++ server?
The generation and use of cookie and session mechanisms have been introducedCPP background serverPublic number view), however, seems to be in our C++ background development process met very few;
So how does session work on our server?
First, let’s look at a requirement:
-
After setting login for the first time, customers want to use quick login or one-click login when they log in again. For example, we use fingerprint login to obtain our account information
Solution:
Description:
-
When the user login quickly, according to the quick login ID code in Redis search
-
If it does not exist in redis, create a session and bind it to the user ID. If yes, the login succeeds, and relevant functions are invoked to display user information
-
Session generation Generally, each company has its own modified scheme to improve security. In this case, the native MD5 interface is used
Redis key value pair design, generally relatively simple, we suggest you can design a set of their own, and implement this function;
Here is a brief example of sessionID generation:
#pragma once
#include <iostream>
#include <openssl/md5.h>
#include <string.h>
using namespace std;
class Md5
{
public:
Md5();
~Md5();
bool SetMd5(string data);
unsigned char* GetMd5();
private:
MD5_CTX ctx;
unsigned char outMd5[16];
};Copy the code
#include "Md5.h"
Md5::Md5()
{
}
Md5::~Md5()
{
}
unsigned char* Md5::GetMd5() {// Array initializes memset(outMd5,0x00,sizeof(outMd5)); int res = MD5_Final(outMd5,&ctx);if(res ! = 1) { cout<<"Md5_Final is errpr"<<endl;
}
returnoutMd5; } bool Md5::SetMd5(string data) {// Initialize Md5 MD5_Init(&ctx); // calculate Md5 int res = MD5_Update(& CTX,data.c_str(),5);if(res ! = 1) { cout<<"Md5_Update is errpr"<<endl;
return false;
}
return true;
}Copy the code
#pragma once
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
#include "Md5.h"
using namespace std;
class Session
{
public:
Session();
~Session();
Session(string UserName,int ID);
bool SetId();
int GetId();
bool SetUserName();
string GetUserName();
bool SetSessionId();
bool SetSessionData();
string GetSessionData();
unsigned char* GetSessionId();
private:
string name;
int id;
string SessionData;
Md5 md5;
};Copy the code
#include "session.h"
Session::Session()
{
}
Session::~Session()
{
}
Session::Session(string UserName,int ID)
{
this->id = ID;
this->name = UserName;
}
int Session::GetId()
{
return this->id;
}
string Session::GetUserName()
{
return this->name;
}
bool Session::SetSessionData() { char str[20]; memset(str,0,sizeof(str)); Sprintf (STR, sessionID) sprintf(STR, sessionid)"%d",GetId());
SessionData = GetUserName()+str;
return true;
}
string Session::GetSessionData()
{
if(! SessionData.empty())return SessionData;
}
unsigned char* Session::GetSessionId()
{
return md5.GetMd5();
}
bool Session::SetSessionId()
{
bool res = md5.SetMd5(GetSessionData());
if(! res)return false;
return true;
}Copy the code
#include "session.h"
int main()
{
unsigned char* str = new unsigned char[16];
Session session("test", 10); session.SetSessionData(); session.SetSessionId(); str = session.GetSessionId();for(int i=0; i<16; i++) {printf("%02X",str[i]);
}
printf("\n");
return 0;
}Copy the code
CXX = g++ -std=c++11
CFLAG = -g -lssl -lcrypto
target = test
OBJ = Md5.cpp main.cpp session.cpp
$(target):$(OBJ)
$(CXX) -o $@ $^ $(CFLAG)
clean:
rm -f $(target)Copy the code
To learn more about C++ background server, please pay attention to wechat official account: ====CPP background server development ====
Warm prompt
If you like this article, please share it in moments, and for more information, please follow me.
Sweep yards attention
More wonderful