Jump to content

Archived

This topic is now archived and is closed to further replies.

Twentyoneth

cookie problems

Recommended Posts

This script worked fine until I tried to encorporate a cookie variable. This is to display a picture, you can choose a picure and it will auto choose the the picture on reload on each page if the cookie is set. When you choose a picture, it sets the cookie variable to the picture name, minus the extension (.jpg ect...). With this script, I am trying to grab the variable set by the cookie, and use it to load a picture, if the cookie is not set, or the picture the cookie refers to is not there, it will use a default picture. Also, if the cookie is not set, the variable can be set through the address (.php?dp=hehe). Now that I have this incorporated, it discontinues page loading and does not display a picture, the path is bad because its not reading the path correctly. Any help would be greatly appreciated.

[code]<?php

$dp = $_GET['dp'];
$dir = "dp/";
$dpdefault = $HTTP_COOKIE_VARS['DPDefault'];

echo "<a href='dppick.php?dp=" . $dp . "' style='border-width: 0px;' onclick=";
echo '"window.open(this.href, ';
echo "'popupwindow', 'width=356,height=427'); return false;";
echo '"';
echo "'><img style='border-width: 0px;' src='";

   $jpgfile = ($dir . $dp . ".jpg");
   $pngfile = ($dir . $dp . ".png");
   $bmpfile = ($dir . $dp . ".bmp");
   $jpegfile = ($dir . $dp . ".jpeg");
   $giffile = ($dir . $dp . ".gif");
   $default = ($dir . "default.jpg");
   if(isset($dp)) {

      if(is_file($jpgfile)) {
         echo $jpgfile;
        } elseif(is_file($pngfile)) {
         echo $pngfile;
        } elseif(is_file($bmpfile)) {
         echo $bmpfile;
        } elseif(is_file($jpegfile)) {
         echo $jpegfile;
        } elseif(is_file($giffile)) {
         echo $giffile;
        } else {
         echo $default;
        }

     } elseif($isset($_COOKIE['DPDefault'])) {
      $djpg = ($dir . $dpdefault . ".jpg");
      $dpng = ($dir . $dpdefault . ".png");
      $dbmp = ($dir . $dpdefault . ".bmp");
      $djpeg = ($dir . $dpdefault . ".jpeg");
      $dgif = ($dir . $dpdefault . ".gif");
      if(is_file($djpg)) {
         $ddefault = ($dir . $dpdefault . ".jpg");
         echo $ddefault;
        } elseif(is_file($dpng)) {
         $ddefault = ($dir . $dpdefault . ".png");
         echo $ddefault;
        } elseif(is_file($dbmp)) {
         $ddefault = ($dir . $dpdefault . ".bmp");
         echo $ddefault;
        } elseif(is_file($djpeg)) {
         $ddefault = ($dir . $dpdefault . ".jpeg");
         echo $ddefault;
        } elseif(is_file($dgif)) {
         $ddefault = ($dir . $dpdefault . ".gif");
         echo $ddefault;
        } else {
         echo $default;
        }

     }

?>


' width='48' height='48'></img></a>[/code]

Share this post


Link to post
Share on other sites
other than the fact that i think you could have written this code much more efficiently, can you please post the code that sets your cookie?

Share this post


Link to post
Share on other sites
Please keep in mind that I am a newb, and if you could explain a more efficient way of writing this code or any code in general, please tell me how, or explain what I'm doing wrong, and heres the part of my code that sets the cookie.

[code]if($_POST['OK']) {
   setcookie("DPDefault", $dp);
   echo "<body onLoad='window.close(this.href);'></body>";
  }[/code]

Share this post


Link to post
Share on other sites
Ok, it's not a problem with my cokie, its the way I emplimented it into the code. But I don't see whats wrong.

Share this post


Link to post
Share on other sites
you aren't setting an expiration time for your cookie, therefore it automatically expires when you close the browser. without setting an expiration date, it's pretty much the same as doing a session.

for instance:

[code]
$expiretime = time() + 3600;
setcookie("DPDefault", $dp, $expiretime);
[/code]

this will make a cookie called DPDefault that holds whatever value is in $dp, and it will expire in 1 hour from the time it is set.

also, i don't know where in your script you are setting the cookie, but you need to set it before you have any html output whatsoever, even blank lines, or else it won't work.

as far as coding technique: i assume that information you want to store as a cookie is the name of the image, right? well why not save the entire image name, including the extension? that alone will knock out all those extra var sets and if/elseif's you have.

also, you seem to be giving the GET precidence over the cookie. That should probably be the other way around. It's just personal taste and all, but think about it like this: if a user wants to go for the cookie option and save their preference, wouldn't that be the first choice for what they want to see? I don't know what the rest of your code looks like, but having that GET option seems rather superfluous.

in other words, you should first check to see if the cookie exists, and if it does, load the picture based on the cookie. if it doesn't, then load theh default.

Share this post


Link to post
Share on other sites
[EDIT: I have everything working, except when the popup closes, the cookie is no longer read. I have tried echo'ing out, but nothing shows up.

Here is the code that sets the cookie, which is in a popup window:

[code]<html>
<head>
<title>My Display Picture</title>
</head>
<body bgColor='#ffffff' style='margin: 0px'>

<?php

   $dp = $_GET['dp'];

if($_POST['Remove']) {
   $dir = "dp/";
   unlink($dir . $dp);
  }
if($_POST['OK']) {
   $expiretime = time() + 3600;
   setcookie("DPDefault", $dp, $expiretime);
   echo "<body onLoad='window.close(this.href);'></body>";
  }

?>

<table border='0' width='356' cellspacing='0' cellpadding='0' height='399'>
<tr><td vAlign='top' width='100%'>

  <table border='0' width='100%' cellspacing='0' cellpadding='0'>
  <tr><td vAlign='top' width='356' height='51'>
    <img src='images/dppickstrip1.PNG' width='356' height='51'></img>
  </td></tr></table>

</td></tr>
<tr><td vAlign='top' width='100%'>

  <table border='0' width='100%' cellspacing='0' cellpadding='0'>
  <tr><td vAlign='top' width='14' height='248'>
    <img src='images/dppickstrip2.PNG' width='14' height='248'></img>
  </td><td vAlign='top' width='220' height='247'>
    <table border='0' width='100%' cellspacing='0' cellpadding='0'>
    <tr><td vAlign='top' width='220' height='1'>
      <img src='images/dppickstrip3.PNG' height='1' width='220'></img>
    </td></tr>
    <tr><td vAlign='top' width='220' height='246' style='background-image: url(images/dppickstrip4.PNG);'>
      <div style='height: 246px; width: 220px; position: absolute; overflow: auto;'>

<!-- select dp -->
<table border='0' width='100%' cellspacing='0' cellpadding='0'>
<tr><td vAlign='middle' align='left'>

<?php

$dir = "dp/";

$file = opendir($dir);
while(($pics = readdir($file)) !== false) {
      if($pics !== "." & $pics !== "..") {
         echo "<tr><td vAlign='middle' align='left'><a href='?dp=" . $pics . "' style='color: #000000; text-decoration: none; border: 0px;'><img src='dp/" . $pics . "' height='32' width='32' style='border: 0px; padding-left: 5px; padding-right: 2px; padding-top: 1px; padding-bottom: 0px;'></img>" . substr($pics, 0, -4) . "</a></td></tr>";
        }
     }

?>

</table>

<!-- /select dp -->

      </div>
    </td></tr></table>
  </td><td vAlign='top' width='100%'>

    <table border='0' width='100%' cellspacing='0' cellpadding='0'>
    <tr><td vAlign='top' width='122' height='88' style='background-image: url(images/dppickstrip5.PNG);'>
      <form action='<?php echo $PHP_SELF; ?>' method='POST'>
      <table border='0' width='100%' cellspacing='0' cellpadding='0'>
      <tr><td vAlign='top' align='left' style='padding-left: 11px; padding-right: 0px; padding-top: 0px; padding-bottom: 0px;'>
        <input type='submit' value='Browse...' name='Browse' style='width: 73px; height: 21px;' />
      </td></tr>
      <tr><td vAlign='top' align='left' style='padding-left: 11px; padding-right: 0px; padding-top: 7px; padding-bottom: 0px;'>
        <input type='submit' value='Remove' name='Remove' style='width: 73px; height: 21px;' />
      </td></tr></table>
      </form>
    </td></tr>
    <td vAlign='top' width='122' height='96'>
      <table border='0' width='100%' cellspacing='0' cellpadding='0'>
      <tr><td vAlign='top' width='13' height='96'>
        <img src='images/dppickstrip6.PNG' width='13' height='96'></img>
      </td><td vAlign='top' width='96' height='96' bgColor='#ffffff'>

<!-- preview here -->

<img style='border-width: 0px;' src='

<?php

$dp = $_GET['dp'];
$dir = "dp/";
$dppic = ($dir . $dp);
$default = ($dir . "bitchplease.jpg");
$DPDefault = $HTTP_COOKIE_VARS['DPDefault'];
$DPDefualtpic = ($dir . $DPDefault);

if(isset($DPDefault)) {
   echo $DPDefaultpic;
  } elseif(isset($dp)) {

           if(is_file($dppic)) {
              echo $dppic;
             } else {
              echo $default;
             }

  } else {

   echo $default;

  }

?>


' width='96' height='96'></img>

<!-- /preview here -->

      </td><td vAlign='top' width='13' height='96'>
        <img src='images/dppickstrip7.PNG' height='96' width='13'></img>
      </td></tr></table>
    </td></tr>
    <tr><td vAlign='top' width='122' height='64'>
      <img src='images/dppickstrip8.PNG' height='64' width='122'></img>
    </td></tr></table>

</td></tr></table>

<table border='0' width='356' cellspacing='0' cellpadding='0' height='128'>
<tr><td vAlign='top' width='356' height='128' style='background-image: url(images/dppickstrip9.PNG);'>
  <table border='0' width='100%' cellspacing='0' cellpadding='0'>
  <tr><td vAlign='top' align='left' style='padding-left: 13px; padding-right: 0px; padding-top: 17px; padding-bottom: 0px;'>
    <a href='http://nicotinejunkie.cv.vc' target='_blank' style='font-family: sans-serif; font-size: 12px; line-height: 12px; color: blue;'>Your Loser Cigarette Site</a>
  </td></tr>
  <tr><td vAlign='top' align='left' style='padding-left: 13px; padding-right: 0px; padding-top: 7px; padding-bottom: 0px;'>
    <a href='http://vanity.cv.vc' target='_blank' style='font-family: sans-serif; font-size: 12px; line-height: 12px; color:blue;'>The Site You Built For Your Grandmother</a>
  </td></tr>
  <tr><td vAlign='middle' align='left' style='padding-left: 13px; padding-right: 0px; padding-top: 14px; padding-bottom: 0px;'>
    <input type='checkbox' /><font face='sans-serif' style='font-size: 12px;'>Default This Picture</font>
  </td></tr>
  <tr><td vAlign='top' align='right' width='100%' height='100%'>
    <form action='<?php echo $PHP_SELF; ?>' method='POST'>
    <table border='0' height='100%' cellpadding='0' cellspacing='0' style='padding-top: 12px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;'>
    <tr><td vAlign='bottom' height='100%' align='right' style='padding-left: 0px; padding-right: 12px; padding-top: 0px; padding-bottom: 12px;'>
      <input type='submit' value='OK' name='OK' style='width: 73px; height: 21px;' />
    </td><td vAlign='bottom' height='100%' align='right' style='padding-left: 0px; padding-right: 12px; padding-top: 0px; padding-bottom: 12px;'>
      <input type='button' value='Cancel' name='Cancel' style='width: 73px; height: 21px;' onclick='window.close(this.href);' />
    </td><td vAlign='bottom' height='100%' align='right' style='padding-left: 0px; padding-right: 12px; padding-top: 0px; padding-bottom: 12px;'>
      <input type='button' value='Help' name='Help' style='width: 73px; height: 21px;' onclick='window.close(this.href);' />
    </td></tr></table>
    </form>
  </td></tr></table>
</td></tr></table>

</body>
</html>[/code]

Would it be possible that the <body onLoad close> thing is causing it not to set the cookie?

Is there a propper way to close the window after setting a cookie, or is it because I close the popup window, the cookie is lost?

Here is the code that calls the popup window above:
[code]<?php

$dp = $_GET['dp'];
$dir = "dp/";
$default = ($dir . "bitchplease.jpg");
$DPDefault = $HTTP_COOKIE_VARS['DPDefault'];
$DPDefaultpic = ($dir . $DPDefault);

echo "<a href='dppick.php?dp=" . $dp . "' style='border-width: 0px;' onclick=";
echo '"window.open(this.href, ';
echo "'popupwindow', 'width=356,height=427'); return false;";
echo '"';
echo "'><img style='border-width: 0px;' src='";


if(isset($DPDefault)) {
   echo $DPDefaultpic;
  } elseif(isset($dp)) {

           if(is_file($dp)) {
              echo $dp;
             } else {
              echo $default;
             }

  } else {

   echo $default;

  }

?>


' width='48' height='48'></img></a>[/code]

I have taken all the if's out and decided to stick with the whole file name in the address bar. Simpler, and cleaner, easier to deal with. Except this cookie problem. ]

Share this post


Link to post
Share on other sites
your cookie has to be set before any html output, including your <html> and <head> tags or even blank spaces. you can have all the php script you want before it, as long as it doesn't output any html.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.