Once your members are able to sign in, they should have the option to sign out. Create a file called
"logout.php" and if a member is logged in, display this link so that they may access it. Below is the code
used to successfully logout a user:
As you see, we set $_SESSION['logged_in'] to equal 0, because it is set to 1 when they are signed in.
Next, notice we are setting another cookie. Notice though, the setcookie( ); function. We are not setting a
cookie, but deleting the one specified. This is achieved by setting a negative time. In this example I used
time() - 60. This will successfully delete the cookie that was set if the user chose "Remember me."
Finally, a call to session_destroy(); will delete all of the data that is in the current session.
Afterwards, we use header( ); to automatically reload us to a desired page. In most instances, the index or
main page of your site if you so choose.
CONCLUSION
Well, Kudos to those who actually read through the entire tutorial! If you did, you should be able to
grasp the concept pretty easy. I didn't care how long the tutorial would turn out to be. I wanted to make
sure that everyone who read through would be able to have a good start at building a member system for
their own website. I did not write the tutorial in one setting, this is probably a couple of months worth
of 10 minuts free time here and there. So if any part seems repetitive, irrelevant or off topic in any way
just let me know and I can make revisions.
In this tutorial, code for scripts are broken up into alot of parts. So here is the full code for each
script. If anyone has any questions, feel free to join discussion in our AllSyntax.com Forums!
Subject: "session
logged_in is
zero"
Date: May 01 2007 at 10:47 am
I'm a PHP
noob. I
implemented the
script and
everything
looks ok and
works from a
database
standpoint but
I do not think
the session
from the login
is getting set.
After login, I
redirect to a
page called
usercp.php
which I
included the
page header
code in. I
check the
session[logged_
in] befor the
"check
cookie"
and the session
is set to 0.
Make sure your
session code to
recognize
members is in
your
"page
header"
code. Also that
the code in
your page
header is
executed to
check login
status before
anything
else.
You may need to
bring this to
the forums and
start a topic,
post some of
your code and
we can help you
from there.
Subject: "Error in
Login.php"
Date: Jun 14 2007 at 1:03 am
I have looked
over the code
for quite a
bit, can't
figure it
out.
Here is my
error...
Warning: Cannot
modify header
information -
headers already
sent by (output
started at
C:\wamp\www\
MyLogin.php:6)
in
C:\wamp\www\
login.php on
line 27
Warning:
mysql_fetch_obj
ect(): supplied
argument is not
a valid MySQL
result resource
in
C:\wamp\www\
login.php on
line 36
Login Failure:
An error
occured, please
verify your
username and
password are
correct.
if(!$_POST['us
ername'])
die("Erro
r: You must
enter your
username before
logging
in.");
if(!$_POST['pa
ssword'])
die("Erro
r: You must
enter your
password before
logging
in.");
Subject: ""duece&q
uot;"
Date: Jun 14 2007 at 6:06 am
Duece in answer
to your
question i had
the same
problem its to
do with the
"\"
; it messes up
the code, if
you view it in
notepad++
you''ll see
that nearer the
end of that
line it turns
into a comment
and therefore
doesnt read the
");"
; the only way
i found to fix
it was by
adding another
"\"
; so it was
'\\'.
Wether or not
this will still
block them from
using
"\"
; is another
matter.
Subject: "Error login.php"
Date: Jun 14 2007 at 6:14 am
In answer to
your second
question i
couldnt see
much wrong or
different to my
code the only
thing i could
think of was
the header
location maybe
put the precise
location of it
I.E include the
http:// other
then that i
dont know, im
only a php noob
myself. On
another note
i'd like to
thank bs0d for
creating this
tutorial, it
has been really
helpful for me.
Subject: "Discuss in
Forums"
Date: Jun 14 2007 at 4:32 pm
If you have
specific
issues, please
discuss them in
the forums. To
answer your
question, as
the tutorial
states, you may
have to use
ob_start();<
/b> as the
first line of
code on your
page to
eliminate the
"headers
already
sent"
error(s).
Subject: "logout.php"
Date: Jun 22 2007 at 3:28 pm
Ok i belive
that i have the
correct format
for the
logout.php
script. At
first i had the
same 2 similar
problems as
duece and
corrected based
on your advice.
The problem is
not that when i
hit the logout
link i dont
think that it
logs out
instead im am
redirected to
my home page
which is not my
index page. i
also set a part
of my page to
echo the $user
but that is
blank.. before
i loged in i
said welcome
guest but now i
cant tell if
its still
working dut to
the logout
problem. please
advise.. this
is the code for
the whole
logout page.
Subject: "Integrate"
Date: Dec 20 2007 at 10:22 pm
Hi!
Thank you for
your excellent
tutorial. This
is just what
I've been
looking for. I
have a quick
question
though: is it
possible to
integrate this
with your
Comments
Script? Kinda
like this
site~
Subject: "need help with
logout function"
Date: Nov 07 2008 at 9:08 pm
my error is as
follows
Warning:
session_destroy
()
[function.sessi
on-destroy]:
Trying to
destroy
uninitialized
session in
/home/wemwngdt/
public_html/log
out.php on line
6
Warning: Cannot
modify header
information -
headers already
sent by (output
started at
/home/wemwngdt/
public_html/log
out.php:6) in
/home/wemwngdt/
public_html/log
out.php on line
7
ive created a
link on my
member page to
logout with the
following
code:
if(!$_POST['us
ername'])
die("Erro
r: You must
enter your
username before
logging
in.");
if(!$_POST['pa
ssword'])
die("Erro
r: You must
enter your
password before
logging
in.");
Subject: "upload in
000webhost.com"
Date: May 30 2010 at 1:14 pm
Hi, very nice
script! I tried
uploading the
whole script
and my files to
my host at 000webhost.com
with domain http://batchtu
torials.netau.n
et.
I got some
trouble with
db_connect.php.
It gives an
error that it
could not
connect, etc. I
hope someone
can still
contact me,
because I saw
that the most
comments were
from 2007. And
by the way,
I'm a starter
at PHP :)
hey branic729,
I'm also a
starter at PHP
and I use
000webhost.com
too (though I
use their free
hosting). I do
not know if
anyone answered
any of your
questions, but
I used several
tutorials,
including this
one to teach
myself and
eventually put
together a user
system like
this one. If
you are still
looking for
some help you
can email me at
zkniebs@hotmail
.com
Subject: "000webhost"
Date: Jul 19 2010 at 7:32 pm
I'm unfamiliar
with working on
that
host/setup. A
simple email to
the server
admin should
give you the
answer. It
could be the
reference to
the host in the
db_connect
file.