lundi 29 juin 2015

Unserialize() offset error when using CodeIgniter 2.2 Sessions with Objects


I'm trying to debug some old code from CodeIgniter 2.2. When running some data thru Session, I noticed an unserialize error, Message: unserialize(): Error at offset 160 of 163 bytes. After doing some debugging and research, I found out it's a common backslash issue when unserializing data from Sessions.

The serialized data I'm using has objects of data with backslashes in them, which causes the errors to occur. I'm in need of a replacement that can handle standard class objects as well.

Could someone recommend a quick replacement for codeigniter's Session _serialize() and _unserialize() methods?

public function data_test() {

    $input = array(
        (object)array('name' => 'test2', 'desc' => 'bla bla ob/gyn'),
        (object)array('name' => 'test2', 'desc' => 'bla bla ob\\gyn'),
    );
    var_dump($input);

    $data = $this->_serialize($input);
    var_dump($data);

    $result = $this->_unserialize($data);
    var_dump($result);


}



// --------------------------------------------------------------------

/**
 * Serialize an array
 *
 * This function first converts any slashes found in the array to a temporary
 * marker, so when it gets unserialized the slashes will be preserved
 *
 * @access  private
 * @param   array
 * @return  string
 */
function _serialize($data) {
    if (is_array($data)) {
        foreach ($data as $key => $val) {
            if (is_string($val)) {
                $data[$key] = str_replace('\\', '{{slash}}', $val);
            }
        }
    } else {
        if (is_string($data)) {
            $data = str_replace('\\', '{{slash}}', $data);
        }
    }

    return serialize($data);
}

// --------------------------------------------------------------------

/**
 * Unserialize
 *
 * This function unserializes a data string, then converts any
 * temporary slash markers back to actual slashes
 *
 * @access  private
 * @param   array
 * @return  string
 */
function _unserialize($data) {

    $data = unserialize(strip_slashes($data));

    if (is_array($data)) {
        foreach ($data as $key => $val) {
            if (is_string($val)) {
                $data[$key] = str_replace('{{slash}}', '\\', $val);
            }
        }

        return $data;
    }

    return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
}


How to add an optional toppings option to Woocommerce products?


i want to add an optional topping feature to my Woocommerce wordpress food joint site. I have a pizza category and every pizza has three different sizes. Though I can add the toppings with fixed price, but I want to let the users add as many toppings they want and the price for the toppings changes with the size of the pizza. It has been done on several sites and here are two example sites I could found.

Image one

image two

I am using the composite product woocommerce extension but don't know how to use it to get this. Have tried several things, but it doesn't seem to work exactly how I would want to it to, i.e. different topping prices for different sizes. I have about 20 toppings to offer.

Please help.


Do i save time when requesting page via ajax and excluding non ajax parts?


I am implementing page loading through ajax() and I would like to know if I can save some time when requesting a page through the jQuery function .get()

When running the .get() function I pass a parameter that will indicate to my PHP page that it is being requested by AJAX and that it should return only those parts that are marked as such.

For example, all my PHP pages have header and footer functions, so when these pages are called by my AJAX function, the header and footer functions will be skipped.

Will this save my any time and bandwidth in my .get() request and thus delivering the page faster? If not, is there any other way to accomplish this?


How to store values in a data attribute and pass the value with php


I'm trying to figure out how I can store a user_id value in my drag and drop table and pass the value along when dropping the item in a new column.

For instance with my code. I start all the users in the 'Owes' column. When someone pays I want to move them to the appropriate column with my drag and drop table. I am outputting all this information from my database, so when I move someone from 'Owes' to 'Paid' I will need to update the record in the db so it saves in the drag and drop table.

My table is like this..

 <table class="paymentTable" id="dragTable">
        <tr>
            <th class="thPayment">Paid</th>
            <th class="thPayment">Partially Paid</th>
            <th class="thPayment">Owes</th>
        </tr>
        <tr>
            <td class="tdPayment" id="paid">
                            <div>
            <?php
                if ($paid_name == true) {
                    echo $paid_name;
                } else {
                    echo "No one has paid";
                }
            ?>
                            </div>
            </td>
            <td class="tdPayment" id="partially_paid">
            <div>
            <?php 
                if ($partially_paid__name == true) {
                    echo $partially_paid__name . " - " . $partially_paid_amount;
                } else {
                    echo "No one has made a partial payment";
                }
            ?>  
            </div>
            </td>
            <td class="tdPayment" id="owes">
            <div>
            <?php
                if ($owes_name == true) {
                    echo $owes_name;
                } else {
                    echo "Everyone has paid something";
                }
            ?>  
            </div>
            </td>
        </tr>
    </table>

I drag and drop the table cells like this..

 $(function() {
        $( "#paid, #partially_paid, #owes" ).sortable({
          connectWith: ".tdPayment",
          remove: function(e, ui) {
            var $this = $(this);
            var childs = $this.find('div');
            if (childs.length === 0) {
               $this.text("Nothing");
            }
          },
          receive: function(e, ui) {
            $(this).contents().filter(function() {
                return this.nodeType == 3; //Node.TEXT_NODE
             }).remove();
          },
        }).disableSelection();
      });

My php to output the values is like this. I am going to consolidate the php so it is only one db table rather than three, but this conveys what I am trying to do...

<?php
    //Payment Section

        $con = mysqli_connect("localhost", "root", "", "db");
        $paid_run = mysqli_query($con,"SELECT * FROM paid ORDER BY id DESC");
        $partially_paid_run = mysqli_query($con,"SELECT * FROM partial_payment ORDER BY id DESC");
        $owes_run = mysqli_query($con,"SELECT * FROM owes ORDER BY id DESC");
        $paid_numrows = mysqli_num_rows($paid_run);
        $partially_paid_numrows = mysqli_num_rows($partially_paid_run);
        $owes_numrows = mysqli_num_rows($owes_run);

            if($paid_numrows > 0){
                while($row = mysqli_fetch_assoc($paid_run)){
                    $paid_id = $row['user_id'];
                    $paid_name = $row['name'];
                }
            }

        if($partially_paid_numrows > 0){
                while($row = mysqli_fetch_assoc($partially_paid_run)){
                    $partially_paid_id = $row['user_id'];
                    $partially_paid_name = $row['name'];
                    $partially_paid_amount = $row['payment'];
                }
            }

        if($owes_numrows > 0){
                while($row = mysqli_fetch_assoc($owes_run)){
                    $owes_id = $row['user_id'];
                    $owes_name = $row['name'];
                }
            }
    ?>  

How can I store the user_id, so that I can update the db with php in this drag and drop table?


CORS header not set - can I request an image url and then serve it back to myself?


I am attempting to populate a WebGL Earth with meshes that are compiled from images. These images are cross-domain, and hosted on a server where setting the appropriate headers isn't an option. Can I XMLHttpRequest the image urls, and then serve them back to myself via PHP to bypass CORS errors?

Or, more specifically, can I use my own webserver as a proxy to serve img urls back to myself (to get around CORS) in a WebGL context?

EDIT: The real question here is if I can use my own webserver as a proxy to pass the urls, or if I'll have to actually download each image to the server to then use it.


Using FFMpeg av_read_frame in PHP?


I'm hoping to incorporate FFMpeg in a PHP script. My understanding is that the two best solutions are to use the command line via exec() and extract results or use PHP-FFMpeg (http://ift.tt/1cuzO85).

What I want to do is walk through a video frame-by-frame, and I think I need to use av_read_frame(). What's the best way to use that from PHP? I don't think it's available from the command line or PHP-FFMpeg. Should I write a C program (using XCode) to do what I want and call it from my PHP script?

Thanks!


PHP exec() not executing swipl


I want to execute command to run SWI_Prolog. My command generated correctly, means that when I run it in command prompt I'll get the output but when running it through exec($com, $out, $ret) it wont work and the value of $ret is 1.
Generated command is:

swipl -1 -f D:\xampp\htdocs\virastar\files\script.pl -g main(
    'D:\\xampp\\htdocs\\virastar\\files\\prepared_4329.vis',
    'D:\\xampp\\htdocs\\virastar\\files\\4329_final.vis') -t halt

Once again I have to say when I run it directly in command prompt it word correctly.
Any help will highly appreciated.