Skip to content

PHPSession with Golang, compatible with actual PHP Server

License

Notifications You must be signed in to change notification settings

tiket-oss/phpsessgo

Repository files navigation

PHPSESSGO (Experimental)

The project aimed to imitating PHP Session Management in as much aspect as possible. This library may useful to porting PHP to Go without changing the request contracts. Any session parameter that created/modified should be accessible from PHP Server with same session source.

Usage Example

Create new session manager

import (
	"github.com/tiket-oss/phpsessgo"
)

sessionManager := phpsessgo.NewSessionManager( 
	phpsessgo.DefaultSessionName,
	&phpsessgo.UUIDCreator{},
	&phpsessgo.PHPSessionEncoder{},
	phpsessgo.SessionManagerConfig{
		Expiration:     time.Hour * 24,
		CookiePath:     "/",
		CookieHttpOnly: true,
		CookieDomain:   "localhost",
		CookieSecure:   true,
	},
)

Example of HTTP Handler function

func handleFunc(w http.ResponseWriter, r *http.Request) {
	// PHP: session_start();
	session, err := sessionManager.Start(w, r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(err.Error()))
		return
	}
	defer sessionManager.Save(session)

	// PHP: $_SESSION["hello"] = "world";
	session.Value["hello"] = "world"

	// PHP: session_id();
	w.Write([]byte(session.SessionID))
}

Examples

Build and run the examples

# example using golang standard http library
make standard-http-example 

# example using echo web framework
make echo-middleware-example

About

PHPSession with Golang, compatible with actual PHP Server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published