I'm trying to update session variables via some anchor tags to keep track of information to display. I currently am using $_GET variables, but would like to keep a clean address bar as this is a single page app. What I can't figure out is how to update the session variable and have it refresh the page and update the variable.
This is the only way I can think to do it, but it's not working as I expected.
Use a click.php file that grabs the $_GET variable and applies it to a session variable and redirects back to the index.php file.
index.php
<a href="click.php?msv=PhotoCount" >Photo Count</a>
click.php
if ( !empty($_GET['msv']) ) {
$msv = $_GET['msv'];
$_SESSION['meta'] = $msv;
header('Location: /');
}
Any thoughts?
You should start the php session explicitly at the beginning of both index.php and click.php, like this .. only then your session data will persist
<?php session_start(); ?>
If you choose to do it, consider removing session data later using this snippet
<?php session_destroy(); ?>
Add this to the top of index.php:
<?php
if(isset($_GET['msv']) && !empty($_GET['msv'])){
session_start();
$_SESSION['msv'] = $_GET['msv'];
}
?>
and give the target location of the link to the same page. So, there is no need of the second page.
If you are to use both the pages, give the target location of the link to the second page and add the this line at the end of the if statement.
header('Location: /index.php');
Related
I have a global variable called $_SESSION['user_name'] which is set with POST value when I click on the Submit button in a form.
In this php file, I check if $_SESSION['user_name'] is empty.
If yes, I have to file a form and if no, means the session is still going on and it sends you to the profile.
Anyway, after I fill the form, then it compares this variable if it matches another variable and then heads to the profile.php if true.
On this page, I have one button which is meant to "logout" the user.
On pressing, it sends you to another php file where I change the variable
(I have echo-ed it before and it does change it)
but it doesn't forward it back to main page (index.php) and the variable still has the value from the $_POST although it is different in logout.php as I change it.
Code I have at logout.php:
<?php
session_start();
$_SESSION['user_name'] = ""; // makes it empty
echo $_SESSION['user_name'];
?>
And some html where I use meta tag to redirect back to the index.
Now, my question is, how can I forward the changed variable back to the index or form page? Or is there any other way of doing that?
Thanks :)
Try setting your logout.php file to something like this:
<?
require_once('functions.php');
log_out_user();
header("Location: login.php");
exit;
?>
Now in your functions file, for example, try writing this function:
function log_out_user() {
unset($_SESSION['username']);
// You can also use: session_destroy();
return true;
}
Hope this helps!
I have a single page application based on an index.php page where I start the session and display first contents. Then I load via Ajax contents in different divs based on the links that are clicked.
If I want to access the $_SESSION array in the loaded page (eg. to display user's name) I need to put session_start also in the page that I load via Ajax.
Everything works fine this way but when I see the log I see that php is throwing an error each time I load one of the pages via Ajax saying that
"session already started. Ignoring session_start()".
So: on one side I need to put session_start to access the session array but on the other the session_start command is ignored.
From index.php:
<?php
require '../session_handler.inc.php';
require 'global_functions.php';
session_start();
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
A sample ajax call:
function loadContent(sourceUrl){
$(".container").load(sourceUrl);
}
The php I need on top of the called page:
<?php
require '../../session_handler.inc.php';
require '../global_functions.php';
session_start();
If I remove the session_start() the $_SESSION will be unavailable
How can I fix this situation? Can I access the session array from the loaded page without starting session?
It's better to use this line instead of session_start().
you might have more than one session_start and using session between those two.
if(session_id() == '') {
session_start();
}
If you are testing on a local WAMP or Xamp environment, I would recommend you clear your cache, close your browser (Not a single tab) and run your app.
Try this 1:
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
Or
2: If you have php>=5.4.0, better use this:
$status = session_status();
if($status == PHP_SESSION_NONE){
//There is no active session
session_start();
}else
if($status == PHP_SESSION_ACTIVE){
//Destroy current and start new one
session_destroy();
session_start();
}
Hope this helps..
move session_start(); to top of your page
<?php
session_start();
require '../session_handler.inc.php';
require 'global_functions.php';
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
session_handler.inc.php or global_functions.php may already contain session_start() so move the session_start() of index.php to the top. also note that this should be the first statement in index.php
or insert the code below at page top of index.php
<?php
session_start();
?>
and remove the other session_start() in index.php
Page A and Page B both use PHP page template 1, which is storing get_the_title(); in $_SESSION['pagesource'] = get_the_title(); and sending it to another php file.
However, after visiting page A, then going to page B the variable stills shows the pagesource for Page A until I refresh the page. How do I clear the session so the $_SESSION['pagesource'] is available and true for both pages?
I'm using session_start(); on both pages
Thanks
Try this, at the top of Page B (or indeed any page)
<?php
//Remember to start the session on each page
session_start();
//Unsets all session variables without discretion - ony use if strictly necessary
session_unset();
//Destroys the session, again without discretion - ony use if strictly necessary
session_destroy();
//Typical way to unset any variable
unset($_SESSION['pagesource']);
//Create a new $_SESSION['pagesource'] session variable and set it equal to what get_the_page_title() returns
$_SESSION['pagesource'] = get_the_page_title();
//do anything you want with the page title
exit();
?>
I am very much covering all bases there; try this and if if works we can work on refining it
Whenever I go to a page i.e. login page or any other page, I want to save the name of the page in a $_SESSION variable.
login page:
<?php
session_start();
$_SESSION['page'] = 'login.htm';
?>
It works only for the login page and doesnt overwrite in other pages for e.g. home page:
<?php
session_start();
$_SESSION['page'] = "home.htm";
?>
I need the sesssion variable 'page' to hold the last page I was, can anyone help please?
Why not just use $_SERVER['HTTP_REFERER']? This will give you the previous page in PHP, without having to add anything to sessions.
when you navigate to a new page first retrive the saved "back" variable (and use it in a back link/breadcrumbs or something), and then overwrite the sessions "back" variable with the curent page, to have it ready for the next move =)
If all you need is default "back" functionality you should let the browser handle it.
If what you want is something to be used as a breadcrumb following some internal order (or path in a tree) my advice is to let each page "know" the path that leads to it.
If you really need to know from what page the user came from save it to a previous variable before you write over the current variable.
// Make sure user didnt just refresh the page
if ($_SESSION["current"] !== "currentPage.php") {
$_SESSION["previous"] = $_SESSION["current"];
$_SESSION["current"] = "currentPage.php";
}
You're using different keys.. 'page' and 'back'.
Is it not mandatory to use session_start() before using any session variables in PHP?
I tried the following piece of code without declaring session_start() at the beginning, it worked fine.
So, now I'm confused. Please help !!!!
Also, I did not use any $_POST or $_GET to pass $uname to home.php, but still how does it work? If we use include 'home.php' then does it treat login.php and home.php as same page?
// code login.php//
<?
require_once 'db_connect.php';
if (isset($_SESSION ['user_id']) && !empty($_SESSION ['user_id']))
{
$u_name = $_SESSION['user_name'];
include 'home.php';
}
else
{
//some stmt
}
?>
/*******home.php file ****/
<?php
require_once 'dbconnect.php';
$_SESSION['username'] = $u_name;
//echo $_SESSION['username'];
//blah blah
?>
You definitely need it, if session.autostart is not set in php.ini. But you would probably know that then.
Do you not call it in db_connect.php? Also, I'm pretty sure you wouldn't get any errors, the session would just be empty.
If you include a file via php, Session keeps active (as any others variables set too). If you would access this file as new request, you would need to set session_start().
This behaviour is because include and require act like moving the code of the included file into the current one, as you would have typed the code into one single file.
Plus: you don't need to require dbconnect.php twice.
edit: you asked about both files used as the same page - the page is the output given after the whole php code is done. The page itself doesn't care about how many files internally are used for generating it.
Use the session_start () is obligatory for every session in php. Being passed through a variable values is not necessary to make POST or GET the same, since there is already the case the value increment. If not ouver value in the same session is null or blank, if you open the page in the same way the condition is wrong.
(!Isset($_SESSION ['user_id']) &&!Is_Null($_SESSION['user_id']))
isset to check if empty this need! before, IF(!isset($_SESSION['user_id']) and in the second case would be to check if it is not null and void, for a session either exists or does not exist and if a value is set inesistente is null. So correct view is this: is_null($_SESSION ['user_id'])
Importantly, in the login page does not include but redirect to the page. in the case with a header.
Or could do everything in a single page, but it would not be legal to display on a page called login page. The default would be the index, ie if the login stay within a folder, you place it inside the index page and the address of the folder.
The reason for the session can still open is that sometimes the webserver does not realize that erased part of the code and loads of it from the system cache.