Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Login and Logout using Sessions and Cookies (http://www.go4expert.com/articles/login-logout-using-sessions-cookies-t361/)

ted_chou12 25Nov2006 22:33

Re: Login and Logout using Sessions and Cookies
 
by the way, where do I place my html code?

pradeep 27Nov2006 13:11

Re: Login and Logout using Sessions and Cookies
 
Place the HTML preferably after the session PHP code.

ted_chou12 27Nov2006 18:38

Re: Login and Logout using Sessions and Cookies
 
<?php
ob_start();
session_start(); ?>
**********
You mean here?
**********
<? require_once ("functions.php");

if (checkLoggedin())
echo "<H1>You are already logged in - <A href = \"login.php?do=logout\">logout</A></h1>";
else
echo "<H1>You are not logged in - <A href = \"login.php\">login</A></h1></h1>";
?>

pradeep 28Nov2006 10:09

Re: Login and Logout using Sessions and Cookies
 
Yeah, right! But you may need to include or require your files before that, you can include your files, before the session code.

ted_chou12 30Nov2006 19:03

Re: Login and Logout using Sessions and Cookies
 
okay, ive solved that problem, now, if i want to store username and password in another txt file, what would i have to put in the main php page codes?
eg.
Code:

if(file('logindata.txt'))
return true;
else
return false;

so far, i tried file(), fileread(), file_ get_ contents(), include(), require()...etc.. and a bunch of them, but none works, can you give me a suggestion?

pradeep 30Nov2006 21:19

Re: Login and Logout using Sessions and Cookies
 
It would be something like this

Code: PHP

<?
$username = $_SESSION['usr'];
$pwd = $_SESSION['pwd'];

$h = fopen("userdata.txt");
fwrite($h,"$username\n$pwd\n\n");
fclose($h);
?>


ted_chou12 1Dec2006 14:06

Re: Login and Logout using Sessions and Cookies
 
where do i paste this in?
<?$username = $_SESSION['usr'];$pwd = $_SESSION['pwd'];$h = fopen("userdata.txt");fwrite($h,"$username\n$pwd\n \n");fclose($h);?>
and what do the "\n" stand for?

ted_chou12 1Dec2006 14:08

Re: Login and Logout using Sessions and Cookies
 
oh no, sorry, i think i didn't say it clearly, what i wish is to extract the information of username and password FROM the text file, not store them into the txt files. Thanks for understanding.

pradeep 1Dec2006 14:09

Re: Login and Logout using Sessions and Cookies
 
\n stands for newline. Visit http://in2.php.net/types.string for more escape sequences.

pradeep 1Dec2006 14:13

Re: Login and Logout using Sessions and Cookies
 
Well then you have to store the data in text files, in a specific format say user:location:age OR user#ocation#age, so that the data retrieval becomes easy.
All you have to do is to read the text file line by line, and split it by the separator you have chosen (: or #) and then check the username part for a match. Bingo! you got your record.


All times are GMT +5.5. The time now is 05:27.