I am trying to logout a user. I redirect the user to a logout page. This works ok. but in the logout page, i have a link redirecting to the login page, and when i go there, the user is already logged in... i have an echo to print the UserID...and its printing my user id!
This is my logout function:
function logoutUrl(&$facebook){
$params = array('next' => '(remaining url).../logout.php');
return $facebook->getLogoutUrl($params);
this is my login page:
<?php
include_once "fb_functions.php";
$facebook = getFBInstance();
echo getUser($facebook);
session_start();
?>
<body>
<div id="container">
<div id="header">
</div>
<div id="body">
<h2>
<?php if (getUser($facebook)): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
</h2>
</div>
</div>
Related
Im trying to implement a second if statement inside a first statement, all of that inside HTML.
Should work like this:
User logs in
Login Script checks if information is missing (New User)
If so, user needs to add it
If not, just display "welcome back" (Known User)
<?php if ($_SESSION['ID']): $log = 'Logout'; ?> <!-- If we have a session, login button becomes logout button-->
<h1>You are logged in <?php echo $_SESSION['Name']; ?>!</h1>
<?php if($_SESSION['locationcheck'] === False)?> <!-- If login script says location is missing, form comes up to enter location-->
<h2>Please complete by entering your location:</h2>
<!-- FORM TO ENTER LOCATION COMES UP-->
<?php endif; ?> <!-- This endif is unexpected-->
<?php else: $log = 'Login'; ?> <!-- We dont have a session, display login button-->
<h1>You are logged out!</h1>
<div><?php echo '<a href='.$helper->getLoginUrl().'>Login with fb</a>';?></div>
<?php endif ?>
The error I get is that the first endif is unexpected.
I have a multipage and work on mobile, I use PHP session to keep to consistence, I added a logout button on top and session should be cleared and reload the login page. Below is the content of page, login page is redirected after clicking the logout button but session id seem doesn't be cleared. I have already load the logout with data-ajax=false
HTML
<body class="ui-mobile-viewport ui-overlay-a">
<section id="home" data-role="page">
<header data-role="header">
<h1>Summary</h1>
</header>
<article data-role="content" class="ui-content" role="main">
<button href="#signin_section">Sign-in</button><br>
</article> <!-- article content -->
</section> <!-- section home -->
<section id="signin_section" data-role="page">
<header data-role="header">
<h1>Summary</h1>
Logout
</header>
<article data-role="content" class="ui-content" role="main">
<div id="signin">
<form action="checklogin.php" name="form" id="form" method="post">
... (content omitted) ...
</form>
</div>
</article>
</section>
</body>
PHP script of Logout
<?php
session_start();
unset($_SESSION['login']);
session_destroy();
header("Location: http://jetsodev.aimedia.hk/admincheck");
?>
PHP script of Login
<?php
session_start();
if (isset($_SESSION['login'])) {
....
}
?>
You should use session_unset rather than session_destroy, since the latter “does not unset any of the global variables associated with the session, or unset the session cookie.”
I'm developing a php application and i have a problem retaining session values. I have two files, one is a sidebar (sidebar.php) and a home page (home.php). I have included the sidebar on the home page.
There are login controls on the sidebar and i can successfully login. I know it has successfully logged in because it shows me a message Welcome 'username'. But when i go to the home page, the welcome 'username' part is not shown as the session values are destroyed. instead the login form is shown. Why is that?
This is the home page (sidebar.php)
<?php require_once('connections.php'); ?>
<?php
// if the login button is clicked
if (isset($_POST['btnLogin']))
{
$myusername=$_POST['textusername'];
$mypassword=$_POST['textpassword'];
$result=mysql_query("SELECT * FROM users_table WHERE username='$myusername' and password='$mypassword'");
$count=mysql_num_rows($result);
if($count>=1)
{
$_SESSION['username'] = $row["username"] ;
$_SESSION['userid']= $row["ID"];
}
else
{
//Any code here
}
}
?>
<div class="col-md-12 right-aside">
<?php
if (isset($_SESSION['userid']))
{
echo Welcome : " . $_SESSION['username'];
echo " <a href='logout.php'> | Logout</a>";
}
else //if session is not set
{
echo 'Some html for login form';
}
?>
</div>
The following is the home page (home.php)
<?php include("head.php"); ?>
<body>
<div class="container container-body">
<div class="row">
<div class="col-md-9 main-content">
<div class="row">
<p>Some Text Here</p>
</div><!-- /.row -->
</div><!-- /.main-content -->
<div class="col-md-3">
<div class="row">
<?php include 'sidebar.php';?>
</div>
</div>
Are you starting a session in home.php?
You must call session_start() at the first line of every php script in which you want to access session variables.
Try adding this in top of home.
<?php
session_start();
//Then do your work
Then even if session_start is called again in head.php or sidebar.php , it will be ignored, as the session was already started.
I need some links (related to user account) to appear on the index page for the user who logged in. i have a session variable'email'.
i did this but it didn't work.
<div id="left">
left div content
</div>
<div id=-"right">
<?php
if(isset($_SESSION['email']))
{
?>
//show user some links to his account.
<?php
}
else
{
?>
//show login and register forms
<?php
}
?>
</div>
<?php
session_start(); // add this line
if(isset($_SESSION['email']))
{
?>
Link to php manual.
your first statement within the
<?php
session_start();
//followed by rest of the code.
?>
should be
session_start();
Then the further code.
<?php
session_start();
if(isset($_SESSION['login']))
{
include_once('includes/header.php'); ?>
<!DOCTYPE html>
<html>
<body>
<div id="mainframe">
<img src="img/header.png">
<div id="menu">
<?php include_once('includes/navbar.php'); ?>
</div>
<div id="content">
<h3>Shopping Cart</h3>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
else
{
header('location: login.php');
}
?>
Here is my small PhP code I've got at the moment, my login session is $_SESSION['login'].
And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out.
Does anyone know a simple method / solution for this?
Put this somewhere in your if statement.
It will show Logged in as User at right top corner of page
<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
U need to pass username using SESSION variable for the same
write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.
$query = "SELECT name FROM users WHERE password='$password'";
$username = mysql_result(mysql_query($query),0);
$_SESSION['username'] = $username;
than proceed as you are doing
<?php
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}