PHP

access a cookie’s value immediately after calling the setcookie()

If you want to access a cookie’s value immediately after calling the setcookie() you can’t use $_COOKIE. The reason for this is in the nature of the protocol (see https://tools.ietf.org/html/rfc6265). When you use setcookie() it defines a Cookie to be sent along with the rest of the HTTP headers to the client (see http://php.net/manual/en/function.setcookie.php). But $_COOKIE on the other hand contains variables passed to the current script via HTTP Cookies from the client (http://php.net/manual/en/reserved.variables.cookies.php).

When you change $_COOKIE after calling setcookie() – like some answers here recommend – it doesn’t contain only the Cookies from the client any more. This could interferer with assumptions made in third party code used in your application and may result in unwanted site effects. So in general it’s not good practice and it’s only an option when the calls of setcookie() are part of your own code.

A clean and transparent way to get a value set with setcookie() within the same request is to use headers_list() (see http://php.net/manual/en/function.headers-list.php):

function getcookie($name) {
    $cookies = [];
    $headers = headers_list();
    // see http://tools.ietf.org/html/rfc6265#section-4.1.1
    foreach($headers as $header) {
        if (strpos($header, 'Set-Cookie: ') === 0) {
            $value = str_replace('&', urlencode('&'), substr($header, 12));
            parse_str(current(explode(';', $value, 1)), $pair);
            $cookies = array_merge_recursive($cookies, $pair);
        }
    }
    return $cookies[$name];
}
// [...]
setcookie('uname', $uname, time() + 60 * 30);
echo "Cookie value: " . getcookie('uname');

But notice this won’t work in PHP CLI (e.g. PHPUnit). In such a case you could use third party extensions like XDebug (see http://xdebug.org/docs/all_functions#xdebug_get_headers).

Advertisements

Swap two variables value without using third variable in php

Bit in Expression1 Bit in Expression2 Result
0 0 0
0 1 1
1 0 1
1 1 0
Example with two variable by XOR: decimal
$a = 5;               // 5 => 00000101
$b = 6;              // 6 => 00000110
$a = $a ^ $b; // 3 => 00000011   Now XOR of 5 and 6 is 3
$b = $a ^ $b; // 5 => 00000101  Now XOR of 6 and 3 is 5
$a = $a ^ $b; // 6 => 00000110  Now XOR of 3 and 5 is 6

echo $a.” “.$b;  // Output will be $a=6; $b=5

Use of Referencing in php

There are advantages of using references, for example you don’t have to return anything from the function, nor do you have to look to define them as globally accessible.

EXAMPLE:

function lowercase(&$string){
    $string = strtolower($string);
}

$name = 'SAJU';
lowercase($name);
echo $name; // returns saju

regular expression syntax in php

Example Description
‘/hello/’ It will match the word hello
‘/^hello/’ It will match hello at the start of a string. Possible matches are hello or helloworld, but not worldhello
‘/hello$/’ It will match hello at the end of a string.
‘/he.o/’ It will match any character between he and o. Possible matches are helo or heyo, but not hello
‘/he?llo/’ It will match either llo or hello
‘/hello+/’ It will match hello on or more time. E.g. hello or hellohello
‘/he*llo/’ Matches llo, hello or hehello, but not hellooo
‘/hello|world/’ It will either match the word hello or world
‘/(A-Z)/’ Using it with the hyphen character, this pattern will match every uppercase character from A to Z. E.g. A, B, C…
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the characters ab. E.g. matches abc, but not abcc
‘/abc{1,}/’ Matches one or more c character after the characters ab. E.g. matches abc or abcc
‘/abc{2,4}/’ Matches between two and four c character after the characters ab. E.g. matches abcc, abccc or abcccc, but not abc