By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

dont know how to use session in login function

100+
P: 127
I want 2 use session in my login, but i never use session before. How to change my code as below if i want to use session... Can somebody give me some opinion, please.. Thanks a lot.. Below is my php code..

LOGIN FUNCTION
[PHP]<?php
extract ($_POST);

mysql_connect("localhost","root","")
or die("Cannot connect to the server");

mysql_select_db("ums e-job portal")
or die("Cannot connect to the database");

$sql="select username from company where username='$username'";

$result=mysql_query($sql)
or die("Cannot execute query");

$num=mysql_num_rows($result);

if($num==1) //username valid
{
$sql="select username from company where username='$username' and password='$password'";

$result2=mysql_query($sql)
or die("Cannot execute query");

$num2=mysql_num_rows($result2);
if($num2>0)
{
setcookie("Username", $username, time() + 60 * 60 * 24 * 365);
$today= date("D F d, h:ia");

include("welcome.php");

}
else //message send to emp_login.php when wrong password
{
$message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
include("emp_login.php");
}
}
elseif($num==0) //message send to emp_login.php when username not valid
{
$message="The username you entered does not exist! Please try again.<br>";
include("emp_login.php");
}
?>[/PHP]

WELCOME PAGE
[PHP]<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1 align="center"><?php echo $username; ?>, welcome to UMS e-Job Portal!</h1>
<h3 align="center"> You have been logged in since: <?php echo $today; ?>. </h3>
<?php
include("homepage.php");
?>
</body>
</html>[/PHP]
Jan 28 '07 #1
Share this Question
Share on Google+
9 Replies


ronverdonk
Expert 2.5K+
P: 4,258
When you want to use the $_SESSION array, you can do the following:
start each script with command[php]start_session();[/php].
In your login script you can then save, say, the username, e.g.[php]$_SESSION['username'] = $username;[/php].
This variable is available to each script you use after it and has the session_start(); at its beginning. You can extract the username using [php]$myuser = $_SESSION['username'];[/php]
Ronald :cool:
Jan 28 '07 #2

P: 28
Thz bro.........
Jan 29 '07 #3

sandeepvanarase
P: 1
That's not possible
Jan 29 '07 #4

ronverdonk
Expert 2.5K+
P: 4,258
Would be nice to our members if you would explain what exactly is not possible!

Ronald :cool:
Jan 29 '07 #5

P: 28
I already write 3 fles
(1) login.php
(2) login_success.php
(3) logout.php
[php]
(1) Login.php code is here
<?php
ob_start();
include "../db_connection.php";
if(isset($_POST['submit']))
{
$email=($_POST['txtemail']);
$password=($_POST['txtpass']);
$confirm="";
$login="SELECT * FROM table WHERE Email='$email' && Password='$password' '";
$result=mysql_query($login);
$count=mysql_num_rows($result);
if($count==1)
{
session_register("txtemail");
session_register("txtpass");
header('Location: login_success.php');
}
else
{
echo "Error";
}
}
ob_end_flush();
?>
<html>
<body>
<table cellspacing="0" cellpadding="0" border="0">
<form name="frmlogin" method="POST" action="login.php">
<input type="text" name="txtemail"><br>
<input type="password" name="txtpass"><br>
<input type="submit" name="submit" value="Login">
</form>
</table>
</body>
</html>

(2) login_success code is
<?php
session_start();
if(!session_is_registered('txtemail'))
{
header("location: login.php");
}
?>
In this login_success page I want to get Login name from login.php page how to I get..........


(3) logout.php code is
<?
session_start();
session_destroy();

echo "Successfully logout";
?>[/php]
Jan 30 '07 #6

100+
P: 127
Thanks, Ronald.. :) I will try it first, if got any problem, i will post to this forum again.. Thanks a lot..
Jan 31 '07 #7

ronverdonk
Expert 2.5K+
P: 4,258
I already write 3 fles
(1) login.php
(2) login_success.php
(3) logout.php

(1) Login.php code is here
<?php
ob_start();
include "../db_connection.php";

if(isset($_POST['submit']))
{
$email=($_POST['txtemail']);
$password=($_POST['txtpass']);
$confirm="";
$login="SELECT * FROM table WHERE Email='$email' && Password='$password' '";
$result=mysql_query($login);
$count=mysql_num_rows($result);
if($count==1)
{
session_register("txtemail");
session_register("txtpass");
header('Location: login_success.php');
}
else
{
echo "Error";
}
}
ob_end_flush();
?>
<html>
<body>
<table cellspacing="0" cellpadding="0" border="0">
<form name="frmlogin" method="POST" action="login.php">
<input type="text" name="txtemail"><br>
<input type="password" name="txtpass"><br>
<input type="submit" name="submit" value="Login">
</form>
</table>
</body>
</html>
(2) login_success code is
<?php
session_start();

if(!session_is_registered('txtemail'))
{
header("location: login.php");
}

?>
In this login_success page I want to get Login name from login.php page how to I get..........


(3) logout.php code is
<?
session_start();
session_destroy();
echo "Successfully logout";
?>
Where is your session_start() in the login.php?
Next time, when you display code, enclose it within code or php tags!!

Ronald :cool:
Jan 31 '07 #8

P: 28
Where is your session_start() in the login.php?
Next time, when you display code, enclose it within code or php tags!!

Ronald :cool:
i use session_start(); and session_destroy(); in logout.php.
Feb 1 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

register_globals: important note: Since PHP 4.2.0, the default value for the PHP directive register_globals is off, and it is completely removed as of PHP 6.0.0. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.
See the above warning from the PHP documentation. So, as I said before, it is better to use the $_SESSION array fro passing variables. That makes your code e.g.

login.php:[php]session_start();
// at the spot where you do the session_register do this instead:
$_SESSION['txtemail'] = $email;
$_SESSION['txtpass'] = $password;
[/php]
login_success.php:[php]<?php
session_start();
if(!isset($_SESSION['txtemail'))
header("location: login.php");
else
echo "Welcome, ".$_SESSION['txtemail'];
?>[/php]
logout.php code:[php]<?php
session_start();
setcookie(session_name() ,"",0,"/");
unset($_COOKIE[session_name()]);
$_SESSION = array();
session_unset();
session_destroy();

echo "Successfully logged out";
?>[/php]
Ronald :cool:
Feb 1 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.