Click to See Complete Forum and Search --> : login automatickly neraly there help please


redarrow
09-07-2005, 06:07 AM
Hi there all i would like my members in my dating web site to be able to login automatickly like this forum.

example.

a member goes to the dating site they get access to the members area automatickly with out putting in there login deatils how to do this.

thanks please please help here my code.

I'm still plugging away at this over 3 week later... I am looking or help with my current code... I understand the logic of what I want to do just fine... but I'm piecing the code together from reading various sources:

The logic for what's below is thus: If a "keep me logged in" cookie has been set previously and the PHP header on this protected page is checking to see if the cookie exists... then verifies the user name against the DB, and either allows the user access to the page OR destroys the cookie and bumps them back to the login page:
somethink wrong can not see it please help.


<?php
session_set_cookie_params (0, '/', '.mysite.org');
session_start();

MySQL_connect("localhost", '******', '******'); MySQL_select_db("users");

if (isset($_COOKIE['cookname'])) {
$_SESSION['user'] = $_COOKIE['user'];
}

$user = $_SESSION['user'];

$sql = "SELECT user
FROM users
WHERE user = '$user'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the user id is verified,
// set the session
$_SESSION['logged_in'] = true;

}else{

/* Variables are incorrect, user not logged in */
unset($_SESSION['user']);
header('Location: login.php?redir='.$_SERVER['PHP_SELF']);
exit;
}
?>
What's actually happening (and I'm sure there's a good reason... but I am a NooB) is that I'm being kicked back to the login page, even when the cookie is present.

My orginal "check for session login" header worked just fine... but I'm trying to add the "keep me logged in" with cookie feature.

Orginal PHP "check for login session" header, which works fine for standard sessions:





<?php
session_set_cookie_params (0, '/', '.mysite.org');
session_start();

if($logged_in){
}else{
// not logged in, move to login page
header('Location: login.php?redir='.$_SERVER['PHP_SELF']);
exit;
}
?>

bokeh
09-07-2005, 06:51 AM
The reason you can't login later is because the session has expired. If you want to automatically log in next week you will need a cookie. I don't see setcookie() anywhere in your script. In order to read a cookie it must have been set.

redarrow
09-07-2005, 07:02 AM
please can you add this to the code to work thanks.


setcookie("user");

is this the correct set cookie command but were to put it


<?php
session_set_cookie_params (0, '/', '.mysite.org');
session_start();

MySQL_connect("localhost", '******', '******'); MySQL_select_db("users");

if (isset($_COOKIE['cookname'])) {
$_SESSION['user'] = $_COOKIE['user'];
}

$user = $_SESSION['user'];

$sql = "SELECT user
FROM users
WHERE user = '$user'";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the user id is verified,
// set the session
$_SESSION['logged_in'] = true;

}else{

/* Variables are incorrect, user not logged in */
unset($_SESSION['user']);
header('Location: login.php?redir='.$_SERVER['PHP_SELF']);
exit;
}
?>

bokeh
09-07-2005, 08:36 AM
I'm sorry but I don't have time at the moment but I'm sure someone will help you.

Sheldon
09-07-2005, 04:45 PM
you set the cookie with their varibles when the user logs in manualy.
search php.net - cookies (http://nz.php.net/manual-lookup.php?pattern=cookies)