Session Start Then Session Off - php

Hi there I had some problem with my coding. Can you help me, the situation like this. I created one alert box that asks user if they want to exit from system. I want to do so if users want to logout press yes and the session will be terminated but if they press no the session will not be terminated. This was my coding:
<?php
session_start();
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Logout confirmation</title>
<script type="text/javascript">
var userAns;
userAns = confirm("Click OK to logout or CANCEL to stay at this page");
if(!userAns)
window.location= "baseupdate1.php";
else
window.location= "success.php";
</script>
</head>
<body>
</body>
</html>
Can i ask something, my mini project had admin and user . If user login, it will direct to mainpage.php but if admin login it will go to admin.php. Admin.php can view user register, forms, and comments. I had used session to change button from sign up to userinfo at my header
<strong><?php
if(isset($_SESSION['CurrentUser'])){
echo 'USER INFO';
}else
echo '<a href="signup.php">SIGN UP';
?> </strong>
Instead if has session the link will change, can i make if session admin login, will had no userinfo and signup but link admin info that had user register, forms, and comments. Sorry if my english not good. But i will try to make you all understand about it.

Set session during login
session_start();
$_SESSION['login'] = 'value';
In your success.php page add this
if(isset($_SESSION['login']))
{
unset($_SESSION['login']);
session_destroy();
}
I think it will help you.

Related

php allow access to pages to only logged in users

I have 3 pages have html code similar to just example
<!DOCTYPE html>
<html>
<body>
<div>
html code
</div>
<div>
html code
</div>
<div>
html code
</div>
</body>
</html>
and have 2 php files login.php and logout.php , is it possible to strict access to the 3 pages only to login users
First, Put in header page this session_start();, Ofcourse the header page is included||required in every php page you have.
Second, When the user Login using your Login page, Put the sessions if his data are valid
<?php
if($user && password_verify($password, $user['password'])){
$_SESSION['id'] = $user['id'];
}
?>
In this one we used his id inside session, Now all you have to do is checking if this session is active, If it is not active, You redirect the visitor using header() to the index||404 page like this
<?php
if(!isset($_SESSION['id'])){
die(header("location: 404.php"));
}
?>
and remove the ! for signup & login pages, Since you don't want a logged in user to access the login or register page again.
Third, For logout page, Just put
<?php
session_start();
session_unset();
session_destroy();
header("location: index.php");
exit();
?>
inside it
This answer assumes that you have 3 php files and not html. You need to save those files as .php if you want to manage this using PHP.
Yes you can manage that using a variable or session.
You can redirect the user if they are not logged in. Or, you can show the part of the text and link only if they are logged in.
<?php
if($logged_in) {
?>
Only for logged in users
<?php
}
?>
You need to use session to do it in PHP, it will be like this.
$_SESSION(id)
You can see some tutorial in google.
Refer the below link http://www.makeitsimple.co.in/PHP_loginexmp.php

Login page - Goes straight to members page and redirects to login page

I am having this minor problem with the login page. Clicking the login button goes straight to members page and redirects back to login page. Please take a look at my website: http://www.nigelsham.co.uk > click "login".
If you enter the login details:
username: 007
password: Password123!
It shows the users data in members page.
Here is the code:
Login page - http://pastebin.com/8XJepv7d
Members page PHP code:
<?php
session_start();
if (!$_SESSION["myusername"]) {
$ogmeta = '<meta http-equiv="refresh" content="0;url=http://nigelsham.co.uk/login.php">';
echo $ogmeta;
} else{
$user = $_SESSION['myusername'];
$pass = $_SESSION['mypassword'];
$email = $_SESSION['myemail'];
}
?>
Any suggestions?
I have spent a lot of time fixing the SESSIONS, do you think SESSIONS could be part of the problem? Or could it be the $ogmeta variable?
It seems to me that the problem is the login button should be directly to login, right?
So, change the href in the login button to redirect to login.php

Authenticating with PHP $_SESSION

I have a code to log in to a database system and do some simple tasks like view user details, edit user details kind of tasks. After logging in, a user should have the ability to log out. My question arise here. I need to redirect to a page which display some text(logoff.php page) and then redirect to the index page. But once I click the logout link it directly goes to the index page which is the login page.
a user is redirected to the log off page from a page and my logoff.php code is this
<?php
session_start();
if (isset($_SESSION["uname"])) {
unset($_SESSION["uname"]);
}
header("Location:../index.php");
exit;
?>
<html>
<head>
<title>Logged Off</title>
</head>
<body>
<br><br>
<div align="">
<h2>You are now logged off</h2>
home</div>
</body>
</html>
It should show the text if a user is redirected to here. But I am sure that it comes to this page since it shows the text inside html tags when I remove the php part of the code. I think the problem is with $_SESSIONcode segment. Doesn't it authenticate the user? Is that the reason why I am redirected to the index page with out listing the content of the logoff page?
My index page which is used for logging is this.
<?php
session_start();
$mess="";
if(isset($_POST["submit"])&&$_POST["submit"]=="Sign in") {
//conncet to the database
require_once("./dbcon/user.php");
include("./dbcon/dbcon.php"); //database connection function
$user=$_POST["uname"];
$password=md5($_POST["password"]);
//retriving data from db
$query = "SELECT user_name FROM user_info WHERE user_name = '$user' AND password ='$password'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
$name=$row["0"];
}
if(mysql_affected_rows()==0) {
$mess = "<font color=purple size=2><b>Wrong username or password.<br>Please try again.</b></font>";
} else {
$_SESSION["uname"]=$name;
header("Location:./user/user1.php");
exit;
}
}
?>
<html>
<!-- html form -->
</html>
In this case do I need to include index.php page into the logoff.php page because SESSION variable is created in the index.php page? Is that the reason or any other?
Thanks!
The answer is in that line, and is a feature, not a bug :
header("Location:../index.php");
This does the redirection server side so that your user never gets to see the page body as he only receives this header, telling the browser to go the that URL.
instead of doing this, you should either use a javascript redirect function with setTimeout() , or use the meta-refresh which is really indicated in this case I guess :
<meta http-equiv="refresh" content="10;URL=yourindexpage.php" />
Note the 10 in the line here above, it indicates the delay in seconds before the redirection occurs.
You can do like this
<?php
session_start();
if (isset($_SESSION["uname"])) {
unset($_SESSION["uname"]);
}
?>
<html>
<head>
<title>Logged Off</title>
</head>
<body>
<br><br>
<div align="">
<h2>You are now logged off</h2>
home</div>
</body>
</html>

display session message only once during entire user session?

i have a html login form on my site that submits to login.php.
within login.php is a header redirect with a session message which echo's out onto the next page home.php.
what i am trying to do is make it so that this message only runs once and doesnt show again until the user logs in again. at the moment what is happening is the message is showing on each page refresh.
can someone please show me what i can do to sort this, thanks.
code in login.php:
<?php
if (logged_in())
{
$_SESSION['login_message']="<div class=\"login-overlay\">
<h1>Login You In Securely</h1>
</div>";
header("Location:home.php");
}
?>
code in home.php:
<?php session_start();
if(isset($_SESSION['login_message'] ))
echo $_SESSION['login_message'];
unset($_SESSION['loginframe2']) ;
?>
Simply unset the login message once it has been displayed.
if(isset($_SESSION['login_message'] )) {
echo $_SESSION['login_message'];
unset($_SESSION['login_message']);
}
Now if a user has seen the message, it won't be in the session anymore. And once he logs in again, login.php will set the variable again.
Just use and track a variable like $_SESSION['message_displayed']. Set it to true when you first display the message, and only display it if !array_key_exists('message_displayed', $_SESSION)

Session and included pages

My session don't seem to be saved. I have a log in page that saves the users id as a session. But when I leave the page and click on another page the sessions are not saved.
Here are my code. I have a main page called index.php that look like this:
<?php
if(!isset($_SESSION)){
session_start();
}
?>
<!DOCTYPE html> <HTML> <HEAD> </HEAD> <BODY>
<?php
$page_content = 'startpage.php';
if (isset($_GET['link']) && !empty($_GET['link']) ) {
$page_content = $_GET['link'];
}
include "$page_content";
include 'menu.php';
include 'footer.php';
?>
To login I use this page called admin.php. Within this page I have loggin code that works fine.
If the user has written right username and password then this happens:
$_SESSION['user_id'] = $user_id;
print_r($_SESSION);
The code above shows the current sessions variables. But as soon one click on another page the variables are left empty.
At the bottom of index.php are the footer.php page included, and it looks like this:
<?php
if(!isset($_SESSION['user_id']) ){
echo "The sessions are not initiated. ";
}
if (empty ($_SESSION['user_id']) ) {
echo "The sessions are empty. ";
}
echo "<a href='index.php?link=admin.php'>Administration</a>";
?>
In this footer I have put two if questions to check the sessions.
After I have succeded to logg in and clicked on an another page the if questions detect that my session are empty and not initiated.
How should I get the sessions to work?
Perhaps I should add that I use a rent database that use MySql.
Before you can use $_SESSION you should always start it with:
session_start()
Also, session_start() must always be called before you output anything on the page, otherwise it will not work properly

Categories