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]
9 2391
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:
Would be nice to our members if you would explain what exactly is not possible!
Ronald :cool:
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]
Thanks, Ronald.. :) I will try it first, if got any problem, i will post to this forum again.. Thanks a lot..
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:
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.
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:
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |