PHP: cookies via path/domain - php

Is it possible to set the path and domain one time at the beginning of the script and have all future sets conform to the same?
From the docs:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

sure:
function own_setcookie($name, $value, $expire=0) {
setcookie($name, $value , $expire, 'myPath', 'myDomain');
}
then use:
own_setcookie('myName', 'myValue');

Related

Is there a way I can pass the "httpOnly" param to PHP's setcookie function without passing all the "extra" arguments?

From the PHP's setcookie documentation the function signature is
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false ]]]]]] )
If I only want to set the $name, $value and $httponly params, and allow the defaults to take effect for the other params. will I still have to lookup the default values for all the other optional params and set them in order to "reach" the $httponly param? e.g:
<?php
setcookie('double_track', '1234567890', 0, '', '', false, true);
^ ^ ^ ^
(do I have to look
up and specify these
values?)
Is there any way around this?
Native PHP methods are specified like that and you can do nothing about it.
However, you can specify your own wrapper method around the native one:
function setMyCookie($name, $value, $httpOnly)
{
setcookie($name, $value, 0, '', '', false, $httpOnly);
// ^ ^ ^ ^
// (specify default values)
}
Now, you may just use setMyCookie as this:
setMyCookie('double_track', '1234567890', true);

Set php cookie until browser close with root path access

I am creating a cookie from this URL "/component/user" in this way:
setcookie("isLogged", $options['user']->name);
I know it will be removed when the browser close, but doing in this way the cookie will be set up only for the "/component/user" URL instead the whole website, so I did this other one with the intention to make it work for whole the website:
setcookie("isLogged", $options['user']->name, "", "/");
but the cookie is not created, what I need is a cookie that expire when the browser close and also set it up for whole website. What am I doing wrong?
From the docs:
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false ]]]]]] )
Use $expire = 0, as it's a timestamp not a string
setcookie("isLogged", $options['user']->name, 0, "/");

What do Null and true do for new SimpleXMLElement?

I've seen PHP code like this, but I've not found anything that explains it:
$xml = new SimpleXMLElement($url, null, true);
What do null and true do? Is there a way to check to see if the requested $url failed? Please provide an PHP example of the last two options being used. Thanks!
The signature of SimpleXMLElement::_construct is
final public SimpleXMLElement::__construct() ( string $data [, int
$options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool
$is_prefix = false ]]]] )
The default value of third parameter is false, so if you want to set it to true, you will also need to provide the second parameter.
Example:
// Create a SimpleXMLElement object from a URL
$sxe = new SimpleXMLElement('http://example.org/document.xml', null, true);
echo $sxe->asXML();

Setting functions in variables

While looking at someone's code today, I saw this:
public $setCookieFunction = 'setcookie';
$setcookie = $this->setCookieFunction;
Anyone knows how this works?
Thanks!
It's likely invoked like this, with the same parameters as setcookie():
$setcookie( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] );
Or:
$this->$setCookieFunction( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] );
The benefit here is that you can change $this->setcookie to whatever function name you want, and that function will get called across the class.

How do I set the socket timeout in PHP?

I need to set timeout on client.recv in TCP server.
last parameter of fsockopen
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

Categories