Using data-ajax to call new page after reset session - php

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.”

Related

jquery mobile send value in link and get in multipages file

I want to use a single file with multiple pages.
In the #first page I have a link to another page with a value sent via GET:
<a href='#second?ID=10'>
And when triggered, it should show the second page and write the value with php:
<div data-role="page" id="second">
<div data-role="header" data-add-back-btn="true">
<h1>Second page</h1>
</div>
<div data-role="content">
<?php
$ID = $_GET["ID"];
echo $ID;
?>
</div>
</div>
It seems that the php code isn't run when the second page is loaded.
Is there any easy way to do this?

Session variable to show a form or not

I want to show a form only if the previous form is filled and submitted. So, I created a session variable which is initialized when I submit the first form. Like this :
if( $flag)
{
$_SESSION['id_essai'] = $id_essai;
echo $_SESSION['id_essai'];
insertion_base($index_essai,$id_essai,$nom_local_essai,$nombre_traitements,$essai_ou_suivis,$thematique);
}
I can get this var in this file because my echo works and return the value.
After that :
if(isset($_SESSION['id_essai']))
{
echo'
<div id="tab_traitement" class="excel"></div>
<form action="#" method="POST">
<button type="button" class="btn btn-default" id="submit_button_traitement">Submit</button>
</form> ';
}else
echo'Pour saisir un traitement, vous devez d\'abord faire la saisie d\'un essai';
This is my Index.php :
<?php session_start();?>
<!DOCTYPE HTML>
<meta charset="UTF-8">
<head>
<title></title>
</head>
<body>
<?php include('onglets.php');?>
</body>
In the file 'onglets.php' : I've got :
<div class="tabs" id="tabs">
<ul>
<li>Essai</li>
<li>Traitement</li>
<li>ITK PRO</li>
<li>ITK </li>
<li>Parcelle élémentaire</li>
<li>Prélèvement</li>
<li>Analyse</li>
<li>Mesure plante principale</li>
<li>Mesure plante CIPAN</li>
<li>Station Climatique</li>
</ul>
<div id="tabs-1">
<?php include('essai.php');?>
</div>
<div id="tabs-2">
<?php include('traitement.php');?>
</div>
<div id="tabs-3">
<?php include('itk_pro.php');?>
</div>
<div id="tabs-4">
<?php include('itk.php');?>
</div>
<div id="tabs-5">
<?php include('pe.php');?>
</div>
<div id="tabs-6">
<?php include('prelevement.php');?>
</div>
<div id="tabs-7">
<?php include('analyse.php');?>
</div>
<div id="tabs-8">
<?php include('mpp.php');?>
</div>
<div id="tabs-9">
<?php include('mpc.php');?>
</div>
<div id="tabs-10">
<?php include('station_climatique.php');?>
</div>
</div>
<div id="tabid"></div>
And I call ajax_insert_essai.php in essai.php. In ajax_insert_essai.php, I initialize $_SESSION['id_essai'].
And I have my session_start() at the beginning without something before so that's not the probem. Please, some help!
How can I do ?
This is because you use ajax. Javascript is client side and php is server side.
The page with the form is not recharged, so it isn't processed by the server, so you won't be able to see the session variable unless you refresh the page
Two solutions :
Refresh the page after submitting the first form or use a client-side method, with javascript (better imo)

php - can't retain session values when navigating to another page

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.

Facebook PHP SDK 3.0 logout fully does not work

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>

PHP - Remove Element If Not Logged In

I'm using the same header.php and footer.php for php files on the backend of a CMS so when a user wants to login, they'll a part of that header.php that I don't want them to see as it's part of the backend e.g.
<section id="sidebar">
<p>Not to be seen</p>
</section>
<section id="content">
<p>Login here</p>
</section>
Is there a PHP function that if a user is logged in, can see the #sidebar and those who are not logged in will remove it. Using display: none; is pointless as it still visible if somebody was to remove that CSS style.
Update:
I found out how to do it. The function was
<?php if(($user = Users::authed()) !== false): ?>
<section id="sidebar">
<p>Not to be seen</p>
</section>
<?php endif; ?>
<section id="content">
<p>Login here</p>
</section>
Thanks to those who answered!
The answer depends on how you login the user i.e. using cookies or sessions or something else.
The basic workflow will however be
if(user_logged_in())
echo "<section id=\"sidebar\">
<p>Not to be seen</p>
</section>";
you must have to try this scenario with native session i hope it will work for you
if(isset($_SESSION['username'])){
<section id="sidebar">
<p>Not to be seen</p>
</section>
}else{
<section id="content">
<p>Login here</p>
</section>
}
The very simplest form.
Once logged in populate session variable like $_SESSION["UserID"] with some user id or username
Modify your code like follows:
<section id="sidebar">
<p>Not to be seen</p>
</section>
<?php
if ($_SESSION["UserID"]) {
?>
<section id="content">
<p>Login here</p>
</section>
<?php
}
?>

Categories