PHP session encoder/decoder written in Go

php_session_decoder

PHP session encoder/decoder written in Go
Build Status

Installation

Install:

  • The recommended way to install is using gonuts.io:

    nut get yvasiyarov/php_session_decoder for more information, please, go to the http://www.gonuts.io/yvasiyarov/php_session_decoder

  • Using default go get tool:

    go get github.com/yvasiyarov/php_session_decoder

Getting started

Example: load php session data from redis:

if sessionId, err := req.Cookie("frontend"); err == nil {
    if sessionData, err := redis.Get("PHPREDIS_SESSION:" + sessionId.Value); err == nil {
        decoder := php_session_decoder.NewPhpDecoder(sessionData.String())
        if sessionDataDecoded, err := decoder.Decode(); err == nil {
            //Do something with session data  
        }
    } else {
        //Can not load session - it can be expired
    }
}

Example: Encode php session data:

data := make(PhpSession)
data["make some"] = " changes"
encoder := NewPhpEncoder(data)
if result, err := encoder.Encode(); err == nil {
    //Write data to redis/memcached/file/etc
}

Copyright

2013-2014 Yuriy Vasiyarov
2014 Yuriy Vasiyarov, Maksim Naumov.

All rights reserved.

Comments
  • Use bytes.Buffer for string concatenation to improve performance

    Use bytes.Buffer for string concatenation to improve performance

    Ran into a performance issue when trying to serialize large nested data. Changing the code to use a bytes.Buffer for string concatenation helped speed up the performance.

    For example:

    // build up ["VALUES": ["ID_0": 0, ..., "ID_9999": 9999]]
    subData := php_serialize.PhpArray{}
    data := php_serialize.PhpArray{
        "VALUES": subData,
    }
    
    for i := 0; i < 10000; i++ {
        subData["ID_"+strconv.Itoa(i)] = i
    }
    
    startTime := time.Now()
    php_serialize.Serialize(data)
    log.Printf("Time took: %s", time.Since(startTime).String())
    

    Before: ~2s After: < 15ms

  • Fix bug when decoding multiple arrays

    Fix bug when decoding multiple arrays

    The decoder did not recognize trailing semi-colons when decoding arrays. Thus, a session like this:

    array1|a:1:{s:5:\"test1\";b:1;};array2|a:1:{s:5:\"test2\";b:1;};
    

    would result in a PhpSessionData map with the keys array1 and ;array2. This change will optionally read the trailing semi-colon after decoding an array, ensuring that the two keys are array1 and array2.

  • Add support for encoding null values

    Add support for encoding null values

    The encoder silently fails when encoding null values. This pull requests adds support for encoding null values, and adds tests to make sure null values are correctly decoded and encoded.

  • Your own decode function for object that implements Serializable interface

    Your own decode function for object that implements Serializable interface

    1. Decode objects with Serializable interface and store its raw data;
    2. You can have your own decode function for object that implements Serializable interface;
    3. Improve NULL decoding
  • Doesn't build on 32 bit platforms

    Doesn't build on 32 bit platforms

    When building packages depending on this one on 32 bit platforms, the following error occurs:

    # github.com/pydio/cells/vendor/github.com/yvasiyarov/php_session_decoder/php_serialize
    vendor/github.com/yvasiyarov/php_session_decoder/php_serialize/unserialize.go:266:19: UNSERIALIZABLE_OBJECT_MAX_LEN (un
    typed int constant 10737418240) overflows int
    

    Please fix this bug.

  • Programm Panics

    Programm Panics

    Programm panics then decoding flowing string "|C040000000000000000000:"

    panic: runtime error: makeslice: len out of range [recovered]
        panic: runtime error: makeslice: len out of range
    
    goroutine 29 [running]:
    testing.tRunner.func1(0x8203a0e10)
        /usr/local/Cellar/go/1.5/libexec/src/testing/testing.go:450 +0x171
    github.com/yvasiyarov/php_session_decoder/php_serialize.(*UnSerializer).decodeString(0x82032baa0, 0x2200000022, 0x217000, 0x0, 0x0)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/php_serialize/unserialize.go:133 +0xe4
    github.com/yvasiyarov/php_session_decoder/php_serialize.(*UnSerializer).readClassName(0x82032baa0, 0x0, 0x0)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/php_serialize/unserialize.go:273 +0x48
    github.com/yvasiyarov/php_session_decoder/php_serialize.(*UnSerializer).decodeSerialized(0x82032baa0, 0x0, 0x0)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/php_serialize/unserialize.go:197 +0x42
    github.com/yvasiyarov/php_session_decoder/php_serialize.(*UnSerializer).Decode(0x82032baa0, 0x0, 0x0, 0x0, 0x0)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/php_serialize/unserialize.go:66 +0x159
    github.com/yvasiyarov/php_session_decoder.(*PhpDecoder).Decode(0x8203a7eb0, 0x82032baa0, 0x0, 0x0)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/decoder.go:41 +0x149
    github.com/yvasiyarov/php_session_decoder.TestFuzzCrashers(0x8203a0e10)
        /Users/gregory/Documents/gospace/src/github.com/yvasiyarov/php_session_decoder/fuzz_test.go:20 +0x1fb
    testing.tRunner(0x8203a0e10, 0x2b4680)
        /usr/local/Cellar/go/1.5/libexec/src/testing/testing.go:456 +0x98
    created by testing.RunTests
        /usr/local/Cellar/go/1.5/libexec/src/testing/testing.go:561 +0x86d
    
Nfc-sun-decoder - A Decoder for NXP 424 DNA SUN (Secure Unique) messages

NFC SUN Decoder A Decoder for NXP 424 DNA SUN (Secure Unique) messages This library makes decoding 424 DNA SUN messages easier. While the 424 DNA chip

Aug 24, 2022
An optimal, byte-aligned, LZ+RLE hybrid encoder, designed to maximize decoding speed on NMOS 6502 and derived CPUs
An optimal, byte-aligned, LZ+RLE hybrid encoder, designed to maximize decoding speed on NMOS 6502 and derived CPUs

TSCrunch TSCrunch is an optimal, byte-aligned, LZ+RLE hybrid encoder, designed to maximize decoding speed on NMOS 6502 and derived CPUs, while keeping

Dec 21, 2022
Golang binary decoder for mapping data into the structure

binstruct Golang binary decoder to structure Install go get -u github.com/ghostiam/binstruct Examples ZIP decoder PNG decoder Use For struct From file

Dec 17, 2022
BPG decoder for Go (Zero Dependencies)

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa BPG for Go BPG is defined at: http://bellard.o

Sep 7, 2020
Sqlyog-password-decoder - Simple decode passwords from .sycs file (SQLyog export connections file)

Decode password: ./sqlyog-password-decoder -str password -action decode Encode p

Nov 21, 2021
A simple Slack message tool for the CLI written in Go

heka A simple Slack message tool for the CLI written in Go Report Bug · Request

Jan 16, 2022
COBS implementation in Go (Decoder) and C (Encoder & Decoder) with tests.

COBS Table of Contents About The project COBS Specification Getting Started 3.1. Prerequisites 3.2. Installation 3.3. Roadmap Contributing License Con

May 22, 2022
PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Dec 30, 2022
Pbm - Package ppm implements a Portable Bit Map (PBM) image decoder and encoder written in Go

Package pbm import "github.com/slashformotion/pbm" Package pbm implements a Portable Bit Map (PBM) image decoder and encoder. The supported image col

Jan 5, 2022
A Session Encoder For Golang

Session_Encoder Version 1125 Support 3 examples (math,calculator,rank) based on communications between Server and Client via Channel Support basic ses

Nov 25, 2021
Fast, secure and efficient secure cookie encoder/decoder

Encode and Decode secure cookies This package provides functions to encode and decode secure cookie values. A secure cookie has its value ciphered and

Dec 9, 2022
Package polyline implements a Google Maps Encoding Polyline encoder and decoder.

go-polyline Package polyline implements a Google Maps Encoding Polyline encoder and decoder. Encoding example func ExampleEncodeCoords() { coords :=

Dec 1, 2022
Rich TIFF/BigTIFF/GeoTIFF decoder/encoder for Go (Pure Go/Zero Dependencies)

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa TIFF for Go Features: Support BigTiff Support

Nov 16, 2022
WebP decoder and encoder for Go (Zero Dependencies).
WebP decoder and encoder for Go (Zero Dependencies).

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa webp ██╗ ██╗███████╗██████╗ ██████╗ ██║

Dec 28, 2022
Fixed width file parser (encoder/decoder) in GO (golang)

Fixed width file parser (encoder/decoder) for GO (golang) This library is using to parse fixed-width table data like: Name Address

Sep 27, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Jan 6, 2023
Pure Go Brotli encoder and decoder

This package is a brotli compressor and decompressor implemented in Go. It was translated from the reference implementation (https://github.com/google

Dec 28, 2022
asciigrid is a Go package that implements decoder and encoder for the Esri ASCII grid format, also known as ARC/INFO ASCII GRID.

asciigrid asciigrid is a Go package that implements decoder and encoder for the Esri ASCII grid format, also known as ARC/INFO ASCII GRID. Install go

Jul 3, 2022
Pure Go encoder/decoder of the QOI image format

QOI - The “Quite OK Image” format for fast, lossless image compression package and small utilities in native Go, quite OK implementation See qoi.h for

Nov 12, 2022
Go encoder and decoder for the NetBPM/PNM image formats. Compatible with Go's image packages.

gpnm This package implements an encoder and decoder for PNM image formats. It can be used with Go's image library. It covers all formats as defined by

Nov 26, 2021