PDA

View Full Version : Aide !



JayT
04-30-2008, 11:55 PM
Dear Aunt Fanny:

:)


Programming in PHP 5, I want to save all the settings from an input interface form in a cookie.

It's an astronomy interface. It holds the date and time and other essential settings that can be used for computations by all the other programs on the site. Rather than have to reenter the same data repeatedly for a series of computations on different pages, I would prefer to save the settings in a common cookie, then all the programs that need the data can easily share it.

However, I get errors that the headers have already been sent and the program malfunctions when I try to set a cookie from the interface form.



What I need to do is:

* ... On loading a page with an input interface, check to see if a cookie exists.

2 ... If the cookie exists, reload the previous interface settings from it.

* ... If not, then set the default values in the interface and them save them to a ***** cookie.

4 ... When any change is made to the interface settings, the updated settings should be saved to a cookie when the [SUBMIT] button is clicked, which also re*****es the page.



Simple trivial cookie examples from tutorial sites, like visit counters work OK.

I just can't make it work for anything more useful, like storing and recalling the settings of an interface via a cookie.

Grrrrr

It works perfectly at home (IIS v5 web server).

After sending the page to my web site host (UNIX Apache server), then it fails, telling me the headers have already been sent and the cookie never gets set and all the programs fail because the cookie data never got set.

Reading the cookie is easy. Attempting to set the cookie gives the error, but only when I put the page on the Internet, not on my home server, where I develop, test and debug my work before posting it on the web.

All the PHP code is prior to the start of any HTML code itself, which is what tutorials say is mandatory.

This is my first serious attempt to use cookies in my PHP work and it is driving me bonkers!

Any ideas ?


.

SyntaXmasteR
05-01-2008, 11:58 AM
"headers have already been sent" is the error associated with outputting something to the screen before session_start() is called. So if you are posting this to a freehost, they are probably outputting their ads, or some type of content which will not allow you to start any type of session.

Look for any print() or echo() commands or any HTML before the session_start() occurs. This is where your problem lies.

JayT
05-01-2008, 04:38 PM
I think I got it solved. No idea what I did. Other than rewrite it differently out of frustration.
LOL

I was at first under the now apparently erroneous impression that I could simply read/save any cookie at any point in the program, just like storing a number in the memory of a calculator as needed.

The main thing that confused me was that it worked on my test server at home but not at the web host.

Now it works on both equally well.

It's not a free host. I have my own .COM and .INFO domains. The .INFO is dead though. Need a new host because the current one is insane. Nothing worked, so I deleted the content.

There is no session_start() command anywhere in the code. The print goes just before the HTML page start.

The test output is simply stored in a string variable contained within a <PRE> block.

Remember, I'm still a novice at this. Sessions are still over my head at this point, except that I know that 'session' is Spanish slang for menstruation - so if you cross the border southward ... :&#222;


However, I think I solved the problem, but it eludes me what I did.

Doncha just HATE it whan that happens? grrrrr

I simply rewrote the blasted thing following a different, but equally warped logic, and suddenly it seems to work.

At least I get no errors anymore and the original theory seems to be working according to expectations.

It is a template I need as a starting foundation upon which to build an interactive interface for entering data for a system of related high-precision astronomy programs and only having to have one central page where the settings are remembered and don't need to be reentered when going to another page (program) that needs the same data. A cookie is the simplest way to do this as far as I know. It works for me.

You can test it in action here and verify if it works for you or not. It's working for me.
http://www.neoprogrammics.com/kookie.php


THE THEORY IS:

Upon loading the page, if there is no cookie data, then the default values are set to zero and saved to the cookie.

When the [Submit] button is clicked, if working properly, whatever values are in the interface text boxes are saved in a cookie and the page re*****es, remembering the updated values of the text boxes.

Upon returning to the site in future, the previous interface values should be restored from the cookie as long it hasn't been deleted or more than *65 days have passed since the last visit.

When moving from page to page on the web site, any program needing the interface data can simply read it directly from the existing cookie that was saved when the interface was last updated.





Below follows the complete test program code (kookie.php) that seems to be working now. I should now be able to make the interface as complex as needed, adding more objects, and be able to save/restore the settings using a cookie. A very simple concept.




<?PHP

// PHP CODE ZONE *

// Name of this running script.
$_ACTION_FILE_NAME_ = $_SERVER[$SCRIPT_NAME];

// Name of the cookie to be stored.
$CookieName = "Test_Cookie";

// Read text data from existing cookie, if any.
$CookieText = trim(@$_COOKIE[$CookieName]);

// Read [SUBMIT] button. Initially, this will return an empty string,
// since the page was not loaded by clicking the button. If the button
// was clicked, this will hold the word 'submit' (or the button value)
// and the page should re***** and the cookie updated with the current
// interface settings. This is not limited to only text box values.
$SubmitButton = trim(@$_COOKIE[$Submit_Button]);




// DO THIS IF COOKIE DATA EXISTS

if ($CookieText != "")

{

// Split cookie data into working array.
$CookieData = split("[ ]", $CookieText);

// Restore interface values from the cookie data array. In this
// case, the interface settings consists of two simple text boxes.
$text* = $CookieData[0];
$text2 = $CookieData[*];

// Copy contents of text boxes into cookie data string
// in preparation for storing in a cookie.
$CookieText = "$text* $text2";

}

else


// DO THIS IF COOKIE IS EMPTY

{
// Set all initial default values.
$text* = "0";
$text2 = "0";

// Copy contents of text boxes into cookie data string
// in preparation for storing in a cookie.
$CookieText = "$text* $text2";
}





// ------------------------------------
// DO THIS IF SUBMIT BUTTON WAS CLICKED

if ($Submit_Button != "")
{
// Read current contents of text boxes. If empty, set default = zero.
$text* = trim(@$_POST['text*']); if ($text* == "") {$text* = "0";}
$text2 = trim(@$_POST['text2']); if ($text2 == "") {$text2 = "0";}

// Copy contents of text boxes into cookie data string
// in preparation for storing in a cookie.
$CookieText = "$text* $text2";
}



// Save the current interface data in a cookie set to expire in *65 days.
SetCookie ($CookieName, $CookieText, time()+*65*86400, "/");

// END OF PHP BLOCK


// HTML PAGE FOLLOWS

print <<< _HTML

<!DOCTYPE HTML PUBLIC "-//W*C//DTD HTML 4.0* Transitional//EN"
"http://www.w*.org/TR/html4/loose.dtd">

<HTML>

<HEAD>

<TITLE>Interface Cookie Test</TITLE>


<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=utf-8">

</HEAD>

<BODY>

<FORM NAME="form*" METHOD="post" ACTION="$_ACTION_FILE_NAME_">

Enter Number X &nbsp; <INPUT TYPE="text" NAME="text*" VALUE="$text*" MAXLENGTH="*6">
<BR>
Enter Number Y &nbsp; <INPUT TYPE="text" NAME="text2" VALUE="$text2" MAXLENGTH="*6">
<BR>
<BR>
<INPUT TYPE="submit" NAME="Submit_Button" VALUE="submit">

</FORM>

<PRE>Current cookie data = "$CookieText"</PRE>


</BODY>

</HTML>


_HTML;

// END OF HTML PAGE

// PHP CODE ZONE 2 FOLLOWS





?>


P.S.
And it is W*C compliant. The Gods of Mount W*C are pleased. There will be no earthquakes this week.

SyntaXmasteR
05-01-2008, 05:29 PM
You should definitely look into sessions. They are actually very simple. I wrote a very very basic tut on sessions here:

http://www.syntax******.info/scripts/secure_session_control.php

Unlike cookies the variables can not be altered by the user.

JayT
05-01-2008, 06:35 PM
You should definitely look into sessions. They are actually very simple. I wrote a very very basic tut on sessions here:

http://www.syntax******.info/scripts/secure_session_control.php

Unlike cookies the variables can not be altered by the user.



Thanks for the tut.

Very helpful.

This is my first experience with passing data between pages and using simple cookies was the simplest way I could figure out how to do it at my present level of experience.

That's all I wanted. To be able to save and restore data.

One question.

Can I return a week later and the settings still be there as long as the cookie is not deleted?

I need a cookie that will last at least until some programmer defined expiration period.

Certain things, like an amateur astronomer's longitude and latitude are generally constant. Once set, they should remain that way until a new value replaces it, even if it is a month later.

For example, almost all the astronomy computations I do are for my home location. Ideally, once I set the values, they should still be there next week, unless I either delete the cookie or it expires.

In this particular case security is not a critical issue. Nothing sensitive in the cookie. Just a simple calculation program in which you enter some numbers and a result is returned.


I want to make a template to store interface settings in a cookie until updated, deleted or expired, whichever comes first.

SyntaXmasteR
05-01-2008, 11:55 PM
Your probably better off with cookies then. A database would be your best option to store the data for long periods for each user for the reason of users deleting cookies pretty often. I normally delete my cookies and temp files once a day just to speed things up while browsing.

JayT
05-02-2008, 12:59 AM
Your probably better off with cookies then. A database would be your best option to store the data for long periods for each user for the reason of users deleting cookies pretty often. I normally delete my cookies and temp files once a day just to speed things up while browsing.

Yes.

I was also thinking of a database for better permanance.

Been wanting to dabble in MySqL.

I love pain.

LOL

If one can make a database for a dumb guest book, one can also make a database for user settings too.

Excellent idea.

I plan to learn a lot over the next few days in Remedial Computer Detention Hall.

Now I want to make programs that can interact.


All your database are belong to us!


Support water***rding for education!
Hey, don't knock it - it works!
My kid was doing calculus by age *!


.

martinuk
05-17-2008, 06:49 PM
thanks man max respact