/    Sign up×
Community /Pin to ProfileBookmark

login using sessionid or time

hi all,
i have written a code in php using sessions.below is the code……

[CODE]
<?php
if(!isset($_SESSION))
{
session_start();
}
$now = time();
$limit = $now;
if(isset($_SESSION[‘last_activity’]) && ($_SESSION[‘last_activity’] < $limit))
{
$_SESSION = array();
header(‘Location:logout.php’);
exit;
}
else
{
//the current time
$_SESSION[‘last_activity’] = $now;
}
?>
[/CODE]

.
tell me how to modify in the above code that if the user is entering for first
time then using time() or sessionid it should be stored
or else if he is entering for first time then using current time() or sessionid
it must be stored…..

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Sep 20.2011 — hi all,

i have written a code in php using sessions.below is the code......

[CODE]
<?php
if(!isset($_SESSION))
{
session_start();
}
$now = time();
$limit = $now;
if(isset($_SESSION['last_activity']) && ($_SESSION['last_activity'] < $limit))
{
$_SESSION = array();
header('Location:logout.php');
exit;
}
else
{
//the current time
$_SESSION['last_activity'] = $now;
}
?>
[/CODE]
.

tell me how to modify in the above code that if the user is entering for first

time then using time() or sessionid it should be stored

or else if he is entering for first time then using current time() or sessionid

it must be stored.....[/QUOTE]

I'm not sure what the question is here... Could you be more specific?
Copy linkTweet thisAlerts:
@ravi951authorSep 21.2011 — i want to check when the new user enters the session that must be

registered in to database or else if the user is old one then it must match in the database so that he need not to register again.....
Copy linkTweet thisAlerts:
@criterion9Sep 21.2011 — i want to check when the new user enters the session that must be

registered in to database or else if the user is old one then it must match in the database so that he need not to register again.....[/QUOTE]


So you are trying to create a login script as well?

http://www.tutorialized.com/view/tutorial/Create-a-Log-in-out-System-or-Script-for-a-Webpage.-UPDATED/69498
Copy linkTweet thisAlerts:
@ravi951authorSep 21.2011 — no not like that.to check whether session exists or not if exists then using the time() it should be stored in the database

or else if exists then continue using the previoue time function or session id
Copy linkTweet thisAlerts:
@criterion9Sep 21.2011 — no not like that.to check whether session exists or not if exists then using the time() it should be stored in the database

or else if exists then continue using the previoue time function or session id[/QUOTE]


So then you would insert your query here:

//the current time

$_SESSION['last_activity'] = $now;
[/quote]


I would assume you already have your database setup and everything...
Copy linkTweet thisAlerts:
@criterion9Sep 21.2011 — This part is where you can do stuff for when a session is new:

if(!isset($_SESSION))

{

session_start();

}
[/quote]
Copy linkTweet thisAlerts:
@ravi951authorSep 21.2011 — i have done that one. tell me whether the below code is correct or not.
[CODE]
<?php
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
$result = mysql_query("SELECT * from login where username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "'");
$rows=mysql_num_rows($result);
if(isset($_SESSION['username']))
{
echo "UserId : ".$_SESSION['username'];
unset($_SESSION['username']);
}
else
{
echo "Set the username";
$_SESSION['username'] = time();
}
if ($rows > 0)
{
session_register('username');
$_SESSION['username'] = $_POST['username'];
header("Location:products.php");
exit;
}
else
{
//unsuccessful login
header("Location:login3.php");
exit;
}
?>
[/CODE]
Copy linkTweet thisAlerts:
@criterion9Sep 21.2011 — i have done that one. tell me whether the below code is correct or not.
[CODE]
<?php
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
$result = mysql_query("SELECT * from login where username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "'");
$rows=mysql_num_rows($result);
if(isset($_SESSION['username']))
{
echo "UserId : ".$_SESSION['username'];
unset($_SESSION['username']);
}
else
{
echo "Set the username";
$_SESSION['username'] = time();
}
if ($rows > 0)
{
session_register('username');
$_SESSION['username'] = $_POST['username'];
header("Location:products.php");
exit;
}
else
{
//unsuccessful login
header("Location:login3.php");
exit;
}
?>
[/CODE]
[/QUOTE]

I'm really not sure what you are aiming for... Did you try writing it out at a high level what it is you want the script(s) to do? If you have your birds-eye view of the problem you can then break it down a bit into smaller parts using psuedocode, which I usually leave as comments when I am finally coding the scripts.
Copy linkTweet thisAlerts:
@ravi951authorSep 22.2011 — k let me explain clearly.

first i want to check whether session exists or not.if

not exists then create a session by giving a random no to it..

thats it how to do that
Copy linkTweet thisAlerts:
@criterion9Sep 22.2011 — k let me explain clearly.

first i want to check whether session exists or not.if

not exists then create a session by giving a random no to it..

thats it how to do that[/QUOTE]

This block is where you would do your "new session" stuff.
[code=php]
if(!isset($_SESSION))
{
session_start();
}
if(isset($_SESSION['username']))
{
} else{
//this is a new session
[/code]


session_id() returns the current session id...which php already handles with sufficient randomness.

http://www.php.net/manual/en/book.session.php
×

Success!

Help @ravi951 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...