Cookie Question

Discussion in 'PHP' started by adamjblakey, Apr 19, 2007.

  1. #1
    Hi,

    Right i am working on a project where i need a POST value to add to a cookie which is fine as i have just done:

    
    setcookie("product", $_POST[product]);
    setcookie("product_select",$_POST[product_select]);
    
    Code (markup):
    This works fine, but what i need to do is when this goes to the next section the user can either buy or add another product i need them to be able to add another product to this cookie so on the diplay page it shows all the products they have. Any idea how i am going to do this?

    Cheers,
    Adam
     
    adamjblakey, Apr 19, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    I would store the data in serialized arrays, just keep setting the cookie with the new serial data everytime someone posts or gets to the page.......
     
    krakjoe, Apr 19, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I am afraid you have lost me a bit there. Please can you explain a bit more on how i should go about this.
     
    adamjblakey, Apr 19, 2007 IP
  4. Chamaro Zwinkels

    Chamaro Zwinkels Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can put an array in the cookie. So you can save many things there.

    I think you can better use a database order by ip or user_id
     
    Chamaro Zwinkels, Apr 19, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    no you can't store arrays in cookies.

    I've not so much time today, but I wrote this for you, hopefully you'll see where I'm going with it, I didn't bother to serialize the data it would just be too confusing for you ....

    
    <?php
    error_reporting( E_ALL );
    class cookie
    {
    	function cookie( $name, $expires = null, $path = null, $domain = null, $secure = null )
    	{
    		$this->name = $name;
    		$this->expires = $expires;
    		$this->path = $path;
    		$this->domain = $domain;
    		$this->secure = $secure;
    		
    		if(! $_COOKIE[ $this->name ] ) setcookie( $this->name, "null" );
    	}
    	function addrecord( $data )
    	{	
    	 	return setcookie( $this->name, $this->getcookie( ) . ":" . $data, $this->expires, $this->path, $this->domain, $this->secure );
    	}
    	function delrecord( $data )
    	{
    		return setcookie( $this->name, preg_replace( "#:$data#si", '', $this->getcookie( ) ), $this->expires, $this->path, $this->domain, $this->secure );	
    	}
    	function getcookie( )
    	{	
    		return $_COOKIE[ $this->name ] ? implode(":", array_values( split(":",  $_COOKIE[ $this->name ] ) )) : "null";
    	}
    	function delcookie( )
    	{
    		return setcookie( $this->name, false );
    	}
    }
    
    $cookie = new cookie( 'products' );
    
    if( $_POST )
    {
    	$cookie->addrecord( $_POST['prodid'] );
    	header("location: " . $_SERVER['PHP_SELF']);
    	exit;
    }
    if( $_GET['delcookie'] )
    {
    	$cookie->delcookie( );
    	header("location: " . $_SERVER['PHP_SELF']);
    	exit;
    }
    if( $_GET['delprod'] )
    {
    	$cookie->delrecord( $_GET['delprod'] );
    	header("location: " . $_SERVER['PHP_SELF']);
    	exit;
    
    }
    ?>
    <html>
    
    <head><title>Cookies and stuff</title>
    <body>
    <h1>Delete from cookie item list</h1>
    <?php
    foreach( split(":", $cookie->getcookie( ) ) as $product )
    {
     	if( $product != 'null' and $product != "" )
    	printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product );
    }
    ?>
    <h1>Add to cookie item list</h1>
    <form action="" method="post" name="theForm">
    <select name="prodid" onchange="document.theForm.submit( document.theForm.prodid.value )">
    	<option value="null">Select Product To Add to cookie</option>
    	<option value="001">Product 1</option>
    	<option value="002">Product 2</option>
    	<option value="003">Product 3</option>
    	<option value="004">Product 4</option>
    	<option value="005">Product 5</option>
    	<option value="006">Product 6</option>
    </select>
    </form>
    <h1>Raw cookie</h1>
    <?php
    print_r( $cookie->getcookie( ) );
    ?>
    
    PHP:
    Maybe you'll be able to use that for inspiration .....
     
    krakjoe, Apr 19, 2007 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    If that is what you knock up when you don't have a lot of time then i would of loved to of seen what you knock up if you do have time.

    I will have to have a look through this and make sense of it all as i am not an expert on PHP so will have to get to grips with your code.

    Thanks a lot for reply.
     
    adamjblakey, Apr 19, 2007 IP
  7. ottodo

    ottodo Guest

    Messages:
    2,055
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Im having difficulties in FF, is it bug in FF?
     
    ottodo, Apr 19, 2007 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    What erros are you getting?

    I am getting several errors myself:

    Notice: Undefined index: products in

    This on multiple lines. Is this what you are getting?

    Cheers,
    Adam
     
    adamjblakey, Apr 19, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    
    <?php
    class cookie
    {
        function cookie( $name, $expires = null, $path = null, $domain = null, $secure = null )
        {
            $this->name = $name;
            $this->expires = $expires;
            $this->path = $path;
            $this->domain = $domain;
            $this->secure = $secure;
           
            if( !@$_COOKIE[ $this->name ] ) setcookie( $this->name, "null" );
        }
        function addrecord( $data )
        {   
            return setcookie( $this->name, $this->getcookie( ) . ":" . $data, $this->expires, $this->path, $this->domain, $this->secure );
        }
        function delrecord( $data )
        {
            return setcookie( $this->name, preg_replace( "#:$data#si", '', $this->getcookie( ) ), $this->expires, $this->path, $this->domain, $this->secure ); 
        }
        function getcookie( )
        {   
            return @$_COOKIE[ $this->name ] ? implode(":", array_values( split(":",  $_COOKIE[ $this->name ] ) )) : "null";
        }
        function delcookie( )
        {
            return setcookie( $this->name, false );
        }
    }
    
    $cookie = new cookie( 'products' );
    
    if( $_POST )
    {
        $cookie->addrecord( $_POST['prodid'] );
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    }
    if( $_GET['delcookie'] )
    {
        $cookie->delcookie( );
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    }
    if( $_GET['delprod'] )
    {
        $cookie->delrecord( $_GET['delprod'] );
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    
    }
    ?>
    <html>
    
    <head><title>Cookies and stuff</title>
    <body>
    <h1>Delete from cookie item list</h1>
    <?php
    foreach( split(":", $cookie->getcookie( ) ) as $product )
    {
        if( $product != 'null' and $product != "" )
        printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product );
    }
    ?>
    <h1>Add to cookie item list</h1>
    <form action="" method="post" name="theForm">
    <select name="prodid" onchange="document.theForm.submit( document.theForm.prodid.value )">
        <option value="null">Select Product To Add to cookie</option>
        <option value="001">Product 1</option>
        <option value="002">Product 2</option>
        <option value="003">Product 3</option>
        <option value="004">Product 4</option>
        <option value="005">Product 5</option>
        <option value="006">Product 6</option>
    </select>
    </form>
    <h1>Raw cookie</h1>
    <?php
    print_r( $cookie->getcookie( ) );
    ?>
    
    PHP:
    Sorry I was being half assed and left error_reporting on which will stop headers for cookies being sent if there are errors which there will be when no cookie has been set before, try that ......
     
    krakjoe, Apr 19, 2007 IP
    adamjblakey likes this.
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Great work, i think i should hopefully be able to add this to my site.

    Thanks for your help.
     
    adamjblakey, Apr 19, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #11
    np, always a pleasure :)
     
    krakjoe, Apr 19, 2007 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Sorry for the late follow up i just have so many projects on at the moment i don't know where my head is at.

    You script works great and have implemented this into my site i just have one question:

    I am going to have 3 fields which i need to add. At present it is just:

    $cookie->addrecord( $_POST['prodid'] );

    I need to add 'image' and also 'product select'

    I am not to sure how this is done, as the addrecord function does not cater for this at the moment.

    Again thanks for your help.

    Cheers,
    Adam
     
    adamjblakey, Apr 23, 2007 IP
  13. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #13
    assuming the stuff is posted just call addrecord again for each thing you need to add to the cookie, or if you wanted to keep it seperate you could just create a new cookie object for each thing you want to track, like

    $products_c = new cookie( 'products' );
    $image_c = new cookie( 'image' );
    $product_select_c = new cookie( 'productselect' );

    those are awful names for objects, just an example......

    If I missed the point then say so and I'll try and answer again .....
     
    krakjoe, Apr 23, 2007 IP
  14. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Thank you for you reply, if i understand correctly would i be doing something like this:

    if( $_POST )
    {
    	
    	$products_c = new cookie( $_POST['prodid'] );
    	$image_c = new cookie( $_POST['image'] );
    	$product_select_c = new cookie( $_POST['image'] );
    
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    }
    Code (markup):
    Then how would i display the items that i have added?

    Cheers,
    Adam
     
    adamjblakey, Apr 25, 2007 IP
  15. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #15
    Ok, right, classes ......

    This syntax

    $blob = new class() ;

    that creates a new instance of that class, not connected to anything you made before, the functions() you see inside the class are refered to as methods, so when you do

    $cookie = new cookie( 'whatever' );

    you create a new cookie object not attached to any others and so when you call functions() [ methods ] on that object they only affect that object, so the same as getting the cookie data from the first cookie you just call getcookie() on each cookie [ object ] you want the data from ....

    Clearer ???
     
    krakjoe, Apr 26, 2007 IP
  16. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #16
    Sorry to bring this one back up after so long i have had several problems with the server for this job.

    I am almost there now, i have got it to add the image to a seperate cookie now like this

    $cookieimage = new cookie( 'image' );
    
        $cookie->addrecord( $_GET['prodid'] );
    	$cookieimage->addrecord( $_GET['image'] );
    
    Code (markup):
    but i just don't know how to loop both the prodid cookie and the image cookie.

    Also i need to know how delete the image cookie when i am deleting the proid.

    This just deletes the product but i need it to include image also.
                   <?php
    		      if( $product != 'null' and $product != "" )
        printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product );
    ?>
    Code (markup):
    My other problem is including the image into the foreach loop here:

    foreach( split(":", $cookie->getcookie( ) ) as $product){
    Code (markup):
    Thank you in advance.
     
    adamjblakey, May 22, 2007 IP
  17. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #17
    Anyone help on this one?
     
    adamjblakey, May 24, 2007 IP
  18. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #18
    Please .... I am almost there with this one i just need a little bit more help.
     
    adamjblakey, May 25, 2007 IP
  19. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #19
    I need more code than that, I can't guess what your code is, post the code you want to change and try to be clearer about how you want to change it ( although I'm sure the above will make sense once I have seen your code )
     
    krakjoe, May 26, 2007 IP
  20. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #20
    Cheers for your reply Joe, sorry for not explaining my requirements correctly.

    Basically what i am trying to achive with this is to have a form where you can submit an image and i size. That is then added to the cart and from there you can add another image/size to the cart as another item in your basket.

    The form that is being submitted is the following:

    
    
    <form action="image-process.php" method="post" enctype='multipart/form-data'>
                          <table width="100%" border="0" cellspacing="0" cellpadding="3">
                            <tr>
                              <td><span class="style1">1. Select The Format </span></td>
                            </tr>
                            <tr>
                              <td><table width="180" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product186" value="186" /></td>
                                    <td><span class="darkgrey_pattern" style="color:#CC0000;">
                                      <label for="product186"><strong>90 x 60 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern" style="color:#CC0000;">
                                      <label for="product186"><strong>&pound;16.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product181" value="181" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product181"><strong>30 x 20 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product181"><strong>&pound;1.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product182" value="182" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product182"><strong>40 x 30 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product182"><strong>&pound;3.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td  width="20"><input type="radio" name="prodid" id="product183" value="183" /></td>
                                    <td width="107"><span class="darkgrey_pattern">
                                      <label for="product183"><strong>60 x 40 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right" width="50"><span class="darkgrey_pattern">
                                      <label for="product183"><strong>&pound;8.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product213" value="213" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product213"><strong>70 x 50 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product213"><strong>&pound;12.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product185" value="185" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product185"><strong>80 x 60 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product185"><strong>&pound;16.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product214" value="214" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product214"><strong>100 x 70 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product214"><strong>&pound;24.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product187" value="187" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product187"><strong>120 x 80 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product187"><strong>&pound;39.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product224" value="224" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product224"><strong>160 x 120 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product224"><strong>&pound;76.99</strong></label>
                                    </span></td>
                                  </tr>
                                  <tr>
                                    <td><input type="radio" name="prodid" id="product190" value="190" /></td>
                                    <td><span class="darkgrey_pattern">
                                      <label for="product190"><strong>200 x 100 cm</strong></label>
                                    </span>&nbsp;</td>
                                    <td align="right"><span class="darkgrey_pattern">
                                      <label for="product190"><strong>&pound;79.99</strong></label>
                                    </span></td>
                                  </tr>
                                </table>
                                  <table width="180" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td width="15"><input type="radio" name="prodid" value="0" id="dropdown_products" /></td>
                                      <td width="165"><select name="product_dropdown" size="1" onchange="pre_select_radio();" style="color:#666666;">
                                          <option value="0" style="color:#000000;">=== More formats ===</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  3:2 formats</option>
                                          <option value="181" >30 x 20 cm - &pound;1.99</option>
                                          <option value="221" >45 x 30 cm - &pound;4.99</option>
                                          <option value="183" >60 x 40 cm - &pound;8.99</option>
                                          <option value="222" >75 x 50 cm - &pound;12.99</option>
                                          <option value="186" >90 x 60 cm - &pound;16.99</option>
                                          <option value="187" >120 x 80 cm - &pound;39.99</option>
                                          <option value="189" >150 x 100 cm - &pound;59.99</option>
                                          <option value="225" >180 x 120 cm - &pound;86.99</option>
                                          <option value="217" >225 x 150 cm - &pound;139.99</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  4:3 formats</option>
                                          <option value="227" >28 x 21 cm - &pound;1.99</option>
                                          <option value="182" >40 x 30 cm - &pound;3.99</option>
                                          <option value="184" >60 x 45 cm - &pound;9.99</option>
                                          <option value="185" >80 x 60 cm - &pound;16.99</option>
                                          <option value="188" >120 x 90 cm - &pound;42.99</option>
                                          <option value="223" >140 x 105 cm - &pound;58.99</option>
                                          <option value="224" >160 x 120 cm - &pound;76.99</option>
                                          <option value="226" >180 x 135 cm - &pound;99.99</option>
                                          <option value="218" >200 x 150 cm - &pound;129.99</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  DIN formats</option>
                                          <option value="195" >A4 (29,7x21 cm) - &pound;1.99</option>
                                          <option value="196" >A3 (42x29,7 cm) - &pound;3.99</option>
                                          <option value="194" >A2 (59,4x42 cm) - &pound;8.99</option>
                                          <option value="193" >A1 (84,1x59,4 cm) - &pound;17.99</option>
                                          <option value="192" >A0 (118,9x84,1 cm) - &pound;39.99</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  Panorama formats</option>
                                          <option value="197" >60 x 20 cm - &pound;3.99</option>
                                          <option value="198" >80 x 20 cm - &pound;5.99</option>
                                          <option value="199" >90 x 30 cm - &pound;9.99</option>
                                          <option value="200" >120 x 30 cm - &pound;12.99</option>
                                          <option value="201" >120 x 40 cm - &pound;16.99</option>
                                          <option value="202" >160 x 40 cm - &pound;22.99</option>
                                          <option value="203" >150 x 50 cm - &pound;29.99</option>
                                          <option value="204" >200 x 50 cm - &pound;39.99</option>
                                          <option value="190" >200 x 100 cm - &pound;79.99</option>
                                          <option value="191" >300 x 100 cm - &pound;129.99</option>
                                          <option value="219" >300 x 150 cm - &pound;179.99</option>
                                          <option value="220" >450 x 150 cm - &pound;269.99</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  Poster formats</option>
                                          <option value="213" >70 x 50 cm - &pound;12.99</option>
                                          <option value="214" >100 x 70 cm - &pound;24.99</option>
                                          <option value="0" style="font-weight:bold;color:#000000;">---  Square formats</option>
                                          <option value="205" >30 x 30 cm - &pound;2.99</option>
                                          <option value="206" >40 x 40 cm - &pound;5.99</option>
                                          <option value="207" >50 x 50 cm - &pound;8.99</option>
                                          <option value="208" >60 x 60 cm - &pound;12.99</option>
                                          <option value="209" >70 x 70 cm - &pound;17.99</option>
                                          <option value="210" >80 x 80 cm - &pound;22.99</option>
                                          <option value="211" >90 x 90 cm - &pound;29.99</option>
                                          <option value="212" >100 x 100 cm - &pound;39.99</option>
                                          <option value="215" >120 x 120 cm - &pound;57.99</option>
                                          <option value="216" >150 x 150 cm - &pound;89.99</option>
                                        </select>                                  </td>
                                    </tr>
                                </table></td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                            </tr>
                            <tr>
                              <td><span class="style1">2. Select The Image </span></td>
                            </tr>
                            <tr>
                              <td><label>
                                <input name="image" type="file" id="image" />
                              </label></td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                            </tr>
                            <tr>
                              <td><span class="style1">3. Upload </span></td>
                            </tr>
                            <tr>
                              <td><label>
                                <input type="submit" name="Submit" value="Start" />
                              </label></td>
                            </tr>
                          </table>
                        </form>
    
    
    Code (markup):
    This submits to image-process.php which is:

    <?php 
    
    
    //path where to store images
    $path_thumbs = "upload/thumbs";
    $path_big = "upload/big";
    
    //the new width of the resized image.
    $img_thumb_width = 160; // in pixcel
    
    $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
    //allowed Extensions
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
    
    
    //check if folders are Writable or not
    //please CHOMD them 777
    if (!is_writeable($path_thumbs)){
       die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
    }
    if (!is_writeable($path_big)){
        die ("Error: The directory <b>($path_big)</b> is NOT writable");
    }
    
    
    foreach ($_FILES as $image) {
    
    if (strlen($image[name])>0) {
    
           $file_type = $image['type'];
           $file_name = $image['name'];
           $file_size = $image['size'];
           $file_tmp = $image['tmp_name'];
    
           //check file extension
           $ext = strrchr($file_name,'.');
           $ext = strtolower($ext);
    	   
    		   if (($extlimit == "yes") && (!in_array($ext,$limitedext)) ) {
    			  echo "Wrong file extension1.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
    			  exit();
    		   }
    	   
           //get the file extension.
           $getExt = explode ('.', $file_name);
           $file_ext = $getExt[count($getExt)-1];
    	   
           //create a random file name
           $rand_name = md5(time());
           $rand_name= rand(0,999999999);
           //get the new width variable.
           $ThumbWidth = $img_thumb_width;
    
           //keep image type
           if($file_size){
              if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                   $new_img = imagecreatefromjpeg($file_tmp);
               }elseif($file_type == "image/x-png" || $file_type == "image/png"){
                   $new_img = imagecreatefrompng($file_tmp);
               }elseif($file_type == "image/gif"){
                   $new_img = imagecreatefromgif($file_tmp);
               }
               //list width and height and keep height ratio.
               list($width, $height) = getimagesize($file_tmp);
               $imgratio=$width/$height;
               if ($imgratio>1){
                  $newwidth = $ThumbWidth;
                  $newheight = $ThumbWidth/$imgratio;
               }else{
                     $newheight = $ThumbWidth;
                     $newwidth = $ThumbWidth*$imgratio;
               }
               //function for resize image.
               if (function_exists(imagecreatetruecolor)){
              	 $resized_img = imagecreatetruecolor($newwidth,$newheight);
               }else{
                     die("Error: Please make sure you have GD library ver 2+");
               }
               imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
               //save image
               ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
               ImageDestroy ($resized_img);
               ImageDestroy ($new_img);
               //print message
            }
    		
            //upload the big image
            move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
    
    
       header("Location: product-select.php?prodid=$_POST[prodid]&image=$rand_name.$file_ext");
    }
    }
    
    ?>
    Code (markup):
    When this comes back to the page with the form it then add the cookie as i have put the following code at the top of the page with the form:

    <?php
    include ("db.config.php"); 
    
    	$cookie = new cookie( 'products' );
    	$cookieimage = new cookie( 'image' );
    
        $cookie->addrecord( $_GET['prodid'] );
    	$cookieimage->addrecord( $_GET['image'] );
    	
    if( $_GET['delcookie'] )
    {
        $cookie->delcookie( );
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    }
    if( $_GET['delprod'] )
    {
        $cookie->delrecord( $_GET['delprod'] );
    	$cookieimage->delrecord( $_GET['delimage'] );
        header("location: " . $_SERVER['PHP_SELF']);
        exit;
    
    }
    ?>
    Code (markup):

    The code which is out putting the cookies is:

     <?php
    		      if( $image != 'null' and $image != "" )
        printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $image, $image );
    ?>
                    <br />
                    <?php
    		      if( $product != 'null' and $product != "" )
        printf( "<a href=\"?delprod=%s\">Del %s</a><br/>", $product, $product );
    ?>&nbsp;			            </td>
                  <td width="63%" valign="top">&nbsp;</td>
                </tr>
              </table>
    	<p>
    	  <?php
    }
    }
    }
    
    ?>
    Code (markup):
    Now for some reason this is getting a lot of problems but is adding. When i try to output the data i am getting duplicate items and also i need to add a delete button next to the product to remove from the cart but don't know how to do that so it deletes both the image cookie and the product size cookie.

    Thanks for your help.

    Adam
     
    adamjblakey, May 29, 2007 IP