index.php
<div id="top_right_section">
<div id="head_icon">
<ul class="top">
<li class="hover"><a href="#" onClick="revealModal('modalPage')">Login</a>
</li>
<li><a href="registration.php" class="about">Registration</a>
</li>
</ul>
</div>
</div>
login.php
<?php session_start();
ob_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$username=$_POST['fname'];
$password=$_POST['password'];
//Input Validations
if($username == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location:index.php");
exit();
}
//Create query
$qry=("SELECT * FROM user_detail WHERE password='$pass' AND fname='$user'") or die("Couldn't query the user-database.");
$result=mysql_query($qry);
//check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) > 0) {
//Login Successful
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['uid'];
$_SESSION['SESS_FIRST_NAME'] = $member['user_name'];
$_SESSION['SESS_LAST_NAME'] = $member['password'];
session_write_close();
header("location:index.php");
exit();
}else {
?>
logout.php
<?php session_start();
session_destroy();
location .......
?>
I ve given a light box in the onclick event to login.. i want , if an user is login through that box, he/should redirect to the same index page, & i can do my coding to display his/her name with that, login word should hide and in that place i want to put a log out button.. is dat possibel to do in a same page ????
index.php
<div id="top_right_section">
<div id="head_icon">
<ul class="top">
<li class="hover"><a href="#" onClick="revealModal('modalPage')">Login</a>
</li>
<li><a href="registration.php" class="about">Registration</a>
</li>
</ul>
</div>
</div>
login.php
<?php session_start();
ob_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$username=$_POST['fname'];
$password=$_POST['password'];
//Input Validations
if($username == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location:index.php");
exit();
}
//Create query
$qry=("SELECT * FROM user_detail WHERE password='$pass' AND fname='$user'") or die("Couldn't query the user-database.");
$result=mysql_query($qry);
//check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) > 0) {
//Login Successful
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['uid'];
$_SESSION['SESS_FIRST_NAME'] = $member['user_name'];
$_SESSION['SESS_LAST_NAME'] = $member['password'];
session_write_close();
header("location:index.php");
exit();
}else {
?>
logout.php
<?php session_start();
session_destroy();
location .......
?>
I ve given a light box in the onclick event to login.. i want , if an user is login through that box, he/should redirect to the same index page, & i can do my coding to display his/her name with that, login word should hide and in that place i want to put a log out button.. is dat possibel to do in a same page ????
Share Improve this question edited Dec 16, 2013 at 13:12 IT gIrLy GiRl asked Dec 16, 2013 at 9:51 IT gIrLy GiRlIT gIrLy GiRl 3374 gold badges7 silver badges20 bronze badges 9- Show the modal part of the code – mplungjan Commented Dec 16, 2013 at 9:54
- i ve tried session with another pages.. but now i want to do in a same page.. – IT gIrLy GiRl Commented Dec 16, 2013 at 9:56
- @sarmistha where are you stuck? – Code Lღver Commented Dec 16, 2013 at 10:04
- 1 WAIT I M TRYING YOUR CODE – IT gIrLy GiRl Commented Dec 16, 2013 at 10:13
- @sarmistha If you think that my answer is helpful then mark it as accepted. thanks. And please talk to me I want to tell you, why? – Code Lღver Commented Dec 16, 2013 at 12:04
3 Answers
Reset to default 4At the time of login start the session by using the:
session_start();
Then use this:
<div id="top_right_section">
<div id="head_icon">
<ul class="top">
<?php if(!isset($_SESSION)) { ?>
<li class="hover"><a href="#" onClick="revealModal('modalPage')">Login</a>
</li>
<?php } else {?>
<li class="hover"><a href="#">Logout</a>
</li>
<?php } ?>
<li><a href="registration.php" class="about">Registration</a>
</li>
</ul>
</div>
</div>
Where you are using the query to check the credential use the session if the credentials are correct.
And at the time of logout destroy the session by using the session_destroy();
.
Of course. You can just set a $_SESSION[]
variable and destroy the session if the user logs out. If the session variable is set, then you can show content which should only be shown to logged in users.
You can take a look here for more information.
EDIT: Example:
<?php
session_start();
if(isset($_SESSION['logged_in'])) {
//Here Logout button
//If the button is clicked just do session_destroy() to unset $_SESSION variables
} else {
//Here Login Form
//If login correctly then set $_SESSION['logged_in'] = true;
}
?>
Yeah its quite simple..
All you will do to login will be seting up a session variable to get the user details(say the primary key of the table)
Check like this
if(isset($_SESSION['user_id'])){
Logout and so so on what you want
}else{
Login goes here
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629999a4637046.html
评论列表(0条)