473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dont know how to use session in login function

127 100+
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
9 2389
ronverdonk
4,258 Expert 4TB
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
foekall
28
Thz bro.........
Jan 29 '07 #3
That's not possible
Jan 29 '07 #4
ronverdonk
4,258 Expert 4TB
Would be nice to our members if you would explain what exactly is not possible!

Ronald :cool:
Jan 29 '07 #5
foekall
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
bb nicole
127 100+
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
4,258 Expert 4TB
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
foekall
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
4,258 Expert 4TB
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

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

Similar topics

3
by: Geoff Winsor | last post by:
Hi, I am experiencing a problem with recalling a session variable which stores whether a person is logged in to a "members only" section of a website. This area of the site has been working...
0
by: Billy Boone | last post by:
I have a current web application that utilizes a login to authenticate users into the application. Once I authenticate them, I store away the user's name in a Session variable. I then utilize...
3
by: Tom | last post by:
Hi I have a web application using asp.net and c#. User has to login to the application with his username and pwd. However, I do not allow other user uses the same username and pwd to login, i.e....
10
by: Li Pang | last post by:
Hi, I created a html page from which I give a link to another web site. The new site is opened in a new window. When I opened multiple windows, they all have the same SessionID. I want ot know...
0
by: sasidar.d | last post by:
hi techies I have created a Login web page . It gets the user identity using the window nt authetication. The page is working fine. I have removed the allow anonymous access check box for the...
9
by: Coleen | last post by:
Hi All :-) I am desperately looking for some help/information on how to direct page flow. Forget what I have done - here's what I need to do: I have a large ASPX.Net - VB.Net web application...
3
abdoelmasry
by: abdoelmasry | last post by:
Hi Friends i need help in sessions im passing variables between pages using sessions this is the main page thats mean the home page the main page have table to show messages for users , it have...
2
by: Gordon Burditt | last post by:
I had this idea about preventing session fixation, and I'm wondering what anyone else thinks about it. The idea is, essentially, don't allow session ids that YOUR PHP didn't generate (and aren't...
3
by: mrosado | last post by:
Hi everybody, I have this problem.- The browser launch this two errors: Warning: session_start() : Cannot send session cache limiter - headers already sent (output started at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.