Replies: 2   Views: 3052
Embedhtml file and sessions
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by dna  · 10-08-2016 - 00:21

Is it possible to keep session active when calling another file on the same server using embedHTML?

If I set session in calling program and do this call:

$html = 'http://' . $_SERVER['HTTP_HOST'] . '/getCustomer.php?ID=12345';
$docx->embedHTML($html, array('isFile' => true));

The target file does not have the session variable.


Posted by admin  · 10-08-2016 - 07:10


As you are using an URL to get the access, you create a new PHP session; that's how PHP (and almost any other language) works when you access a server using a function such as as file_get_contents.

We recommend you to get the HTML as a string using PHP Curl or an URL that returns the HTML using a REST API; and then add this HTML to embedHTML.


Posted by dna  · 11-08-2016 - 10:59

I found a way to maintain session using a curl call.

Below is a function I found on the web.

I modified the function to handle session and basic authentication.


//See Updates and explanation at:
function get_remote_data($url, $post_paramtrs=false)
        if (session_status() == PHP_SESSION_NONE) {
        $useragent = $_SERVER['HTTP_USER_AGENT'];
        $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($c, CURLOPT_POST,TRUE);
                curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&".$post_paramtrs );
        curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($c,CURLOPT_USERAGENT, $useragent);
        curl_setopt( $c, CURLOPT_COOKIE, $strCookie );
        curl_setopt($c, CURLOPT_MAXREDIRS, 10);
        $follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true;
        if ($follow_allowed)
                curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
        curl_setopt($c, CURLOPT_REFERER, $url);
        curl_setopt($c, CURLOPT_TIMEOUT, 60);
        curl_setopt($c, CURLOPT_AUTOREFERER, true);
        curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');

        //Optional authentication
        curl_setopt($c, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

        preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si',  $status['url'],$link); $data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si','$1=$2'.$link[0].'$3$4$5', $data);   $data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si','$1=$2'.$link[1].'://'.$link[3].'$3$4$5', $data);
                return $data;
        elseif($status['http_code']==301 || $status['http_code']==302)
                if (!$follow_allowed)
                        if (!empty($status['redirect_url']))
                                if (!empty($m[1]))
                                return  call_user_func( __FUNCTION__, $redirURL, $post_paramtrs);
        return "ERRORCODE22 with $url!!<br/>Last status codes<b/>:".json_encode($status)."<br/><br/>Last data got<br/>:$data";

You get the html content by using one of these calls:

GET request:

$html = get_remote_data(''); // GET request

POST request:

$html = get_remote_data('', "var2=something&var3=blabla" ); // POST request