I have been workinh on the shell of this page for some time now, bits and pieces, i finally got the login script to work while substitutin the cookie aloocations with text being displayed that shoed whether you would have been logged in or not. anyway, i replaced this text with the code to assign a cookie, and all of a sudden it didnt work. i spent five and a half hours trying to geet it to work, and now, the next day it is still troubling me, can someone pleeeeeeze help me ?
some extra info: when i just type in the code to assign the cookie on the main index (Index.php) page, it works fine and displays the user specified as being logged in, but when i run a script on a seperate page (Includes/do_log.php) then when i go back to the main page it isnt there. logouts used to work but now have stopped.
any questions ort anything, feel free to post them
I'v also included a ZIP containing the entire web system (pages).
1) You need to close the MySQL connection you opened for the counter:
the code for this is: mysql_close($link); or just mysql_close(); to close all open links.
This may be confusing links in other pages, and slowing your system down.
2) //setcookie ("ubunreal_user", "buesr", time() + 43200); is this meant to be spelt buesr? or buser?
do_log.php
1) Maybe if you set the cookie when they logged in, you'd get somewhere...
Can't find anything wrong with Backup Of do_log.php
Possible problems:
your browser could be blocking the cookies
your version of PHP might not work with cookies
something could be wrong with the SQL. I wouldn't know without a MySQL dump.
Couldn't spot anything else that was obvious.
Just check all your cross-page variables and MySQL column & table names are the same on the pages.
i know about msql_close();, it isnt affecting my code and i know so cos i have thoroughly tested it for that, and the reason do_log.php has the cookie commentded out is so i dont have to keep re-typing it when i am testing, so i can just run the page and the only think it will do is run the cokie script and not anything else assuming i dont add the URL string variables for the other stuff.
2) //setcookie ("ubunreal_user", "buesr", time() + 43200); is this meant to be spelt buesr? or buser?
no, its just a string i'm assigning to the cookie for testing purposes, i wasnt even trying to spell anything, i just kabbled something in
[i]a bit more of an explanation:[/u] in includes/do_log.php on line 3 is the testcode to assign the cookie from anothwer page other than the main index module. when i run this page top assign the cookie and then return the the main index, the cookie doesnt appear to have been created. the code for checking if the cookie exists is in index.php lines 3 - 7, this checking code works fine, and i know this becos when i copy the setcookie code from includes/do_log.php to the beginning the main index, it works, why does it work when run in the main index module and now from 'do_log.php' ???
BTW. nice neat code there
if u werent being sarcuastic, then thanx i try and keep it that way cos i get confused if i dont, and try to comment stuff that isnt 100% clear iin the mind.
anyway, any help or comment is appreciated on this situation, so thanx in advance
Another common pitfall is that cookies are sent in reverse order (don't ask me why). So if you want to delete a cookie and then send a new one, you have to send the new one first, and then delete the current one. Confusing I know
Originally posted by scoutt ok I am a little lost. you want to see if the cookie is set but you don't check for it.
either one of these
echo $TestCookie;
echo $HTTP_COOKIE_VARS["TestCookie"];
if the value is null or != then you can say no cookie.
or
Common Pitfalls:
Cookies will not become visible until the next loading of a page that the cookie should be visible for.
Cookies must be deleted with the same parameters as they were set with.
have u had a look @ my code, i can do everything fine, except set the cookie. ok, I'll just throw you the problem straight foreward then:
this works fine:
PHP Code:
IF ($action == "login")
{$result = mysql_query("SELECT * FROM Users WHERE uname='$u_name' AND upassword='$u_password'");
IF (mysql_numrows($result) == "1") {ECHO "loggen in ok";}
ELSE {ECHO "Failed to log in, sorry";}}
ELSEIF ($action == "logout")
{setcookie ("ubunreal_user", "", time() - 43200);}
but this doesnt:
PHP Code:
IF ($action == "login")
{$result = mysql_query("SELECT * FROM Users WHERE uname='$u_name' AND upassword='$u_password'");
IF (mysql_numrows($result) == "1") {setcookie ("ubunreal_user", "$ubunreal_user", time() + 43200);}
ELSE {ECHO "Failed to log in, sorry";}}
ELSEIF ($action == "logout")
{setcookie ("ubunreal_user", "", time() - 43200);}
can anyone explain that ? its the code fom inside the INCLUDE file that i use for this login script.
no, no, no, that part is correct, look @ my previous example of what the problem is. The "IF" statement works like a charm, finds whether the user exists with the password, so basically what it does in that statement is that if the user exists then do this, otherwise do this, @ the moment this is just writing words to the screen (eg. u logged in / out) but when i replace that with "setcookie ("ubunreal_user", "$ubunreal_user", time() + 43200);" then it doesnt work. look @ my code carefully and u'll see what i mean.
so which setcookie is not working. if it is the first one fix that function and then if it still doesn't work I am curious as to where $ubunreal_user is coming from. if it is from the database then you will hav to specify what row it had come from. so I would after the query is do a while statement adn get the info from the database so $ubunreal_user gets filled.
lol, my code worked sooo much better
than that, mine works like a dream with the exception of the
cookie problem, i dont think u understand that. my code does
work, but why when i place the cookie assignments in there it
then doesnt work ?
maybe i should leave this problem for a while and come back to it
when i have more experience ? anyway, i have a page that i
can run that will log me in as an "admin" , and the "logout"
button works like a dream
that doesn't make since. you say your code works so much better but once you stick the setcookie code in there it dies. well it could be that your pc doesn't update your cookies right. the setcookie is fine. the only reason it would die is that it can't get the variable it needs. did you delete all your cookies and see if it makes a new one? also I wouldn't even bother with cookies, as sessions are so much more proficient and easier.
Originally posted by scoutt that doesn't make since. you say your code works so much
better but once you stick the setcookie code in there it dies. well
it could be that your pc doesn't update your cookies right. the
setcookie is fine. the only reason it would die is that it can't get
the variable it needs. did you delete all your cookies and see if it
makes a new one? also I wouldn't even bother with cookies, as
sessions are so much more proficient and easier.
well, when i run the script (IF statement for login) it works fine as long as the output is just text written to the screen (ECHO "" but when the output / action is writing a cookie then it doesnt assign the cookie. but if i just assign the cookie outright from another file then then reload the main index, then it works. this is the most confusing thing i have ever come across in my life, it should work.
ok, so you'v created a database named "ubunreal" and 2 fields called "uname" and "upassword" . When you pass 2 variables ("$u_name" & "$u_password") to the code, it searched the specified Databse and displays the relevant output as to whether the user was found or not and sets the cookie if needed.
am i correct in saying that this works for you ?
cos right now i am going to emptly my IE temp files and try once more, if it doesnt work I'm not going to be happy with me.
well kind of. I created a test table. the only thing I changed was the connection and table. and you won't be able to use the header function as it errors out. that is why I use the meta tag and reresh
i'm sure that cookies work, i'v had to create a temperory login
thiny. ("index2.php") so i can just run it and log me in as
someone with out having to go through the ogin screen, this is
the code fir it:
now if that workd then my login screen should, but
when i run this other script (as above) then i have to refresh
the index page before it picks up the cookie as being set.
here u go scoutt, the complete source. now look at "Index2.php", it logs me in fine, but "includes/do_log.php?action=logout" doesnt delete the cookie, cmon, tell me my code is wrong, i dare you
hmmm, I'll get all over it this weekend, have too many assignments to do right now :'( I'll gave it another try, this is really starting to get to me cos i know that my code is correct and is surposed to work. I'm getting a new PC this afternoon (300mhz, 256mb, 3gb), I'll do fresh installs of everything on it and see how it performs. thanx a lot scoutt.
sorry, i forgot to say that i have Opera only chaching pictures, not pages, is this gonna help wit htesting instead of having the page script determine whether it chaches or not ? and does Opera handle Cookies different to IE ?
Hello ubunreal69, as far as programming php/cookies for opera you shouldnt really have any problems with the cookie aspect of things. I've been doing a lot of testing on both IE and Opera and found that Opera is a lot more lenient on coding and doesnt throw up as much as IE does.
You will definitely want to test your code out seriously on IE for any thing it might not like.