Jump to content


Photo

cookie problems


  • Please log in to reply
7 replies to this topic

#1 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 11 May 2006 - 04:44 AM

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.

<?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>


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 May 2006 - 05:53 AM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 12 May 2006 - 07:45 PM

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.

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


#4 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 13 May 2006 - 04:25 AM

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.

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 May 2006 - 05:42 AM

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:

$expiretime = time() + 3600;
setcookie("DPDefault", $dp, $expiretime);

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 13 May 2006 - 07:06 AM

[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:

<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>

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:
<?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>

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. ]

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 May 2006 - 08:20 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 13 May 2006 - 05:25 PM

Thanks, its all settled now




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users