Manish Jangir
Manish Jangir

Reputation: 5437

Convert All Session Values into String

hi i have many session values in my project and i use the syntext for session is

$_SESSION['username'] = $somevalue;

this things is implemented in may pages around 2000 pages. now i want to replace this thing to

$_SESSION['username'] = (string)$somevalue

in all the pages simultaneously. how can i do this in dreamwaver. please help me. there are many different session values used in my pages.

Is there any way to convert all session values into string simultaneosly. i mean any regex method like $_SESSION[.] = (string) like this. or any other method. please tell me .

Thanks.

Upvotes: 0

Views: 6758

Answers (5)

TV-C-1-5
TV-C-1-5

Reputation: 714

json_encode Works for basic needs:

My_Session_As_String = json_encode($_SESSION);

Upvotes: -1

TV-C-1-5
TV-C-1-5

Reputation: 714

You could use: session_encode()

"session_encode — Encodes the current session data as a session encoded string" https://www.php.net/manual/en/function.session-encode.php

*(Must call session_start() before using session_encode().)

session_start();
    
    $_SESSION['login_ok'] = true;
    $_SESSION['nome'] = 'sica';
    $_SESSION['inteiro'] = 34;
    
    echo session_encode();
    
    this code will print
    
    login_ok|b:1;nome|s:4:"sica";inteiro|i:34;

Upvotes: -1

boobiq
boobiq

Reputation: 3024

It depends of what version of PHP you have. For >=5.3, use Peter's version, for <5.3, use

function stringify($item)
{
    return (string)$item;
}

$_SESSION = array_map('stringify', $_SESSION);

Upvotes: 1

chiborg
chiborg

Reputation: 28094

Just in case you want it in your 2000 code files instead of converting the values at runtime in your script: Don't know if Dreamweaver supports regex search and replace and what the backreference chars are. But try replacing this

\$_SESSION\['[^']+'\]\s*=\s*

with this:

$0(string)

The $0 is the backreference to the matched pattern. If that doesn't work, try \0 or \\0 instead.

Upvotes: 0

Peter Krejci
Peter Krejci

Reputation: 3192

array_map function is probably what you are looking for:

$_SESSION = array_map(function($item) { return (string)$item; }, $_SESSION);

PHP 5.3 is required for anonymous function, in earlier versions you have to pass function name as first argument.

Upvotes: 1

Related Questions