Page 1 of 1

PHP Forum posting & cookie setting.

Posted: Wed Jul 16, 2008 11:11 am
by sHobbsWW
Hello I am a rather new developer when it comes to server side programming. I am currently learning and testing different methods for creating cookies, however I ran into this issue.

I am trying to submit a form with contact information to a another page called (cookies.php). Here is the forum:

Code: Select all

 
<form action="cookies.php" method="POST" name="contactForm">
        <h5>Name:: </h5>
        <input type="text" name="ct_ctName" id="ct_ctName" class="ct_nameField" size="30"/>
        <br/>
        <h5>Company:: </h5>
        <input type="text" name="ct_ctCompany" id="ct_ctCompany" size="30"/>
        <br/>
        <h5>Address:: </h5>
        <input type="text" name="ct_ctAddress" id="ct_ctAddress" size="30" maxLength="150" />
        <br/>
        <h5>City:: </h5>
        <input type="text" name="ct_ctCity" id="ct_ctCity"size="30" maxLength="50" />
        <br/>
    <h5>Zip:: </h5>
        <input type="text" name="ct_ctZip" id="ct_ctZip" size="30" maxLength="20" />
        <br/>
        <h5>Phone:: </h5>
        <input type="text" name="ct_ctPhone" id="ct_ctPhone" size="30" maxLength="30" />
        <br/>
        <h5>Email:: </h5>
        <input type="text" name="ct_ctEmail" id="ct_ctEmail" size="30" maxLength="75" />
        <br/>
        <h5>Fax:: </h5>
        <input type="text" name="ct_ctFax" id="ct_ctFax" size="30" maxLength="30" />
        <br/>
        <h5>URL:: </h5>
        <input type="text" name="ct_ctURL" id="ct_ctURL" size="30" maxLength="150" />
        <br/>
        <h5>Subject:: </h5>
        <input type="text" name="ct_ctSubject" id="ct_ctSubject" size="30" maxLength="75" />
        <br/>                                                                    
        <input type="submit" value="submit" name="submit"/>
 
[/size]

I am trying to retrieve the sent information and set them into cookies, here is the code:

Code: Select all

 
<?php
[color=#0000FF]setcookie[/color]([color=#FF0000]'testtest '[/color],'[color=#FF0000]omg'[/color],time()+60);
//set vars from posts
$ct_Name = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctName'[/color]];
$ct_Company = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctCompany'[/color]];
$ct_Address = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctAddress'[/color]];
$ct_City = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctCity'[/color]];
$ct_State = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctState'[/color]];
$ct_Zip = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctZip'[/color]];
$ct_Phone = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctPhone'[/color]];
$ct_Email = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctEmail'[/color]];
$ct_Fax = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctFax'[/color]];
$ct_URL = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctURL'[/color]];
$ct_Subject = [color=#0000FF]$_POST[/color][[color=#FF0000]'ct_ctSubject'[/color]];
 
//set cookies from vars from posts
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctName'[/color], $ct_Name, time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctCompany'[/color],$ct_Company,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctAddress'[/color],$ct_Address,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctCity'[/color],$ct_City,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctState'[/color],$ct_State,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctZip'[/color],$ct_Zip,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctPhone'[/color],$ct_Phone,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctEmail'[/color],$ct_Email,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctFax'[/color],$ct_Fax,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctURL'[/color],$ct_URL,time()+60);
[color=#0000FF]setcookie[/color]([color=#FF0000]'ctSubject'[/color],$ct_Subject,time()+60);
?>
<html>
<head>
    <title>Welcome to my info site.</title>
</head>
<body>
<?php
    echo "Thank you";
    echo [color=#0000FF]$_COOKIE[/color][[color=#FF0000]'ctName'[/color]];
    echo "<br/>";
    echo [color=#0000FF]$_COOKIE[/color][[color=#FF0000]'testtest'[/color]];
?>
</body>
</html>
[/size]

But for some reason the cookies are not being created, I created a test cookie and it worked fine, any ideas? Or any better ways of doing what I am trying to do.

If you are curious to the practicality of this there is currently none.

I did use the search to find threads remotely close to what I am doing, but so far have found none. I will continue to search.

Re: PHP Forum posting & cookie setting.

Posted: Wed Jul 16, 2008 12:56 pm
by sHobbsWW
Update: Firefoxx creates the cookies but safari does not...... :banghead:

Re: PHP Forum posting & cookie setting.

Posted: Wed Jul 16, 2008 1:46 pm
by sHobbsWW
Problem fixed and understood.

Setting a cookie with such short life-span may not register depending on how the browser in use sets the expiration time.

Though I am not sure of the differences between time handling in multiple browsers. Maybe there is a minimum time that must be met for cookies in safari, I am not quite sure.

~Thx Eugene

Re: PHP Forum posting & cookie setting.

Posted: Wed Jul 16, 2008 4:27 pm
by RobertGonzalez
Something that might help you is setting the cookie expiry time once in a variable and then using that single variable. And setting each of the post array variables into cookies seems a bit much. You might want to look at sessions.

As a brief little lesson/test though, you could always do this:

Code: Select all

<?php
$expire = time() + 3600; // One hour
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  foreach ($_POST as $key => $val) {
    if (!empty($val) && !strpos($val, 'submit')) {
      setcookie($key, $val, $expire);
    }
  }
}
?>
And please do not post in all bold letters. That makes it look like you are trying to get a point across or something. :wink: