I have problem with add new session with custom name in wordpress,
I try put this in function.php and it's work :
function myStartSession() {
session_destroy();
session_name('my_session');
session_start();
}
add_action('init', 'myStartSession', 1);
But when put same code in other file like single.php not work anymore
I try put only my function in functions.php and put add_action in single.php but same problem, not work
How i can create session with custom name and call when i need it
Change the code to this:
function myStartSession() {
session_destroy();
session_name('my_session');
session_start();
}
add_action('my_action_name', 'myStartSession');
Then within your template files, like single.php as you mentioned, if you want to call the function add:
do_action('my_action_name');
no need to destroy the session, you just need to check if It's not started and start it
function myStartSession() {
if( ! session_id() ) {
session_start();
}
$_SESSION['custom_name'] = 'value';
}
add_action('init', 'myStartSession', 1);
Related
I have assigned an array variable in session for temporary use. Session variable store customer information before checkout using paypal. After checkout I want to store session variable data in database table. Session Variable exist and i can access through template of theme. But When I try to access that session variable in wp-store plugin. It show blank data.
My session variable is
$_SESSION['userinfo'] = $userinfo;
In template of theme , I can directly access session variable that give perfect output.
var_dump($_SESSION['userinfo']);
Similar way, In plugin it give Blank data
$sesData =$_SESSION['userinfo'];
$lname = $sesData['lname'];
$phone = $sesData['phone'];
$addrs = $sesData['addr'];
$lanc = $sesData['lanc'];
$latc = $sesData['latc'];
Sessions aren't enabled in wordpress by default, so you have to activate php sessions in theme and plugin.
When the user logs out or logs into a different account session should be destroyed.
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id())
session_start();
}
function myEndSession() {
session_destroy ();
}
Add below action to plugin
function register_session()
{
if( !session_id() )
{
session_start();
}
}
add_action('init', 'register_session');
The session I set is lost after the form is submitted.
I had built the session class to set new session, unset and so on. In function.php of wordpress template.
function.php
if (!session_id()) {
session_start();
}
include get_template_directory() . "/custom/session.php";
Session.php
class session {
function __construct() {
}
function set_flashdata($name, $value) {
$_SESSION[$name] = $value;
}
function flashdata($name) {
if (isset($_SESSION[$name])) {
$str = $_SESSION[$name];
return $str;
} else {
return FALSE;
}
}
function userdata($name) {
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
} else {
return FALSE;
}
}
function set_userdata($name, $value) {
$_SESSION[$name] = $value;
}
function unset_userdata($name) {
if (isset($_SESSION[$name])) {
unset($_SESSION[$name]);
}
}
}
I try to set session as :
<?php
$sess = new session();
$sess->set_userdata('sess_name',"some value");
?>
<form action="get_permalink(212);">
//input buttons
</form>
After submit the form it goes to the permalink(212). Then I tried.
<?php
$sess = new session();
$value = $sess->userdata('sess_name');
var_dump($value); //returns false. That means session is lost after form submit. Why?
?>
You need to move session start/resume into your Session's constructor.
Like so:
class session
{
function __construct()
{
if (! session_id()) {
session_start();
}
}
Another thing to mention, every time you'll do new Session you'll be getting an object of the same functionality working with same global variable $_SESSION.
You don't need more than one $session object, it would be a good time to look into Singleton pattern.
You have to call always session_start() for each request.
The mission of session_start() is:
Creates a new session
Restart an existing session
That means, if you have created a session, and you don't call to the method session_start(), the variable $_SESSION is not going to be fulfilled.
Except: If in your php.ini you have set the option session.auto_start to 1, then, in that case it is not needed to call the session_start() because the variable $_SESSION is fulfilled implicitly.
You need to use wordpress global variable for condition that session is set or not something like :
global $session;
if (!session_id()) {
session_start();
}
include get_template_directory() . "/custom/session.php";
It might be due to www. at the start of your website domain. Make sure that both of pages use the same structure.
Also I faced with the same issue long time ago when the form sends the data to a secured address (https://)
I hope these two items may help you.
Sounds to me like session_start() is not set at the start of the page that get_permalink(212;) refers to.
I have almost no experience with WP itself though, so I might misunderstand the functionality of get_permalink()
I agree with the answer from #rock3t to initialize session in constructor of class, but every time a class object is initiated, it will go to check for session!
Instead, if you are fine, the simplest way to get access to session is by adding following lines to your wp-config.php file before the call to wp-settings
if (!session_id())
session_start();
This will set/initialize session globally and you won't need to set/check for session_start in constructor of a class.
Thank you.
Hi iam new to wordpress and I have created a plugin at which I need to print all the session data.First I have created a file in plugin folder and added code like
function myplugin_classname() {
print_r($_SESSION);
}
And I put an click event for two button with class tags like
$('.tags').on('click',function(){
$.post('my_page.php',{val:$(this).val()});
});
and in my_page.php I kept like
$_SESSION['tag'] = $_POST['val'];
but when it comes to printing the session variables at myplugin_classname (by refreshing the page)it doesnt prints the newly assigned session variable....How to solve this..??I have started session through theme-my-login login.
You need to add <?php session_start(); ?> at beginning of my_page.php
After that for destroying session you can use wp_logout action in wordpress. code is as follows
<?php function custom_unset_session() {
// your code
unset($_SESSION['tag']);
}
add_action('wp_logout', 'custom_unset_session');
?>
// On your plugin functions.php
function register_session() {
if (!session_id())
session_start();
}
add_action('init', 'register_session');
function your_function() {
//Here you can unset your session variabl
}
add_action('wp_logout', 'your_function');
//Now you can use
$_SESSION['tag'] = $_POST['val'];
Those two articles are helpful as well:
http://www.frank-verhoeven.com/using-session-in-wordpress/
http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/
I am editing wp-login.php to create a custom login screen. Maybe there's a better way to do this, so if anyone has experience with that any comments are welcome.
Within my theme's functions.php I start a session:
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
Within my theme file I set a session variable:
// Check if we've submitted a language
if($_GET['id'] == 'en') {
$_SESSION['bam_lan'] = 'en';
}
if(!isset($_SESSION['bam_lan'])) {
$_SESSION['bam_lan'] = 'es';
}
// Set language
$bam_lan = $_SESSION['bam_lan'];
Within wp-login.php, echo $_SESSION['bam_lan']; doesn't echo anything.
How do I get a global session variable which is set in my theme's functions.php from wp-login.php??
Thanks!
wp-login.php executes before functions.php and before 'init' action.
I'm creating a bilingual site and have decided to use session_start to determine the language of the page using the following:
session_start();
if(!isset($_SESSION['language'])){
$_SESSION['language'] = 'English'; //default language
}
The problem with this is that it clashes with Wordpress and I get the following:
Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/home/neurosur/public_html/v2/wp-content/themes/default/header.php:8)
in /home/neurosur/public_html/v2/wp-content/themes/default/region.php
on line 13
Is there a way to get around this?
EDIT
Wordpress sends header info before the header.php file is run. So starting the session in the header.php may still conflict with the header info that wordpress sends. Running it on init avoids that problem. (From jammypeach's comment)
Write the following code in your functions.php file:
function register_my_session()
{
if( !session_id() )
{
session_start();
}
}
add_action('init', 'register_my_session');
Now if you want to set data in session, do like this
$_SESSION['username'] = 'rafi';
I've changed my original answer to the correct one from #rafi (see below).
Write the following code in your functions.php file:
function register_my_session()
{
if( !session_id() )
{
session_start();
}
}
add_action('init', 'register_my_session');
I found an interesting article by Peter here. I'm using the following code in my functions.php:
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id()) {
session_start();
}
}
function myEndSession() {
session_destroy ();
}
This destroys old session when user logs out then in again with different account.
Based on this answer and the answers given here, it's best to use the following code snippets:
For versions of PHP >= 5.4.0, PHP 7:
function register_my_session() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
add_action('init', 'register_my_session');
Reference: http://www.php.net/manual/en/function.session-status.php
For versions of PHP < 5.4.0:
function register_my_session() {
if(session_id() == '') {
session_start();
}
}
add_action('init', 'register_my_session');
Reference: https://www.php.net/manual/en/function.session-id.php
I don't want to take any credits, I just felt like sharing this information would be helpful.
for PHP version 7.4
function register_my_session() {
if (session_status() === PHP_SESSION_NONE && session_status() !== PHP_SESSION_ACTIVE) {
#session_start();
}
}
add_action('init', 'register_my_session');
add_action('init', 'customSessionStart', 1);
add_action('wp_logout', 'customSessionDestroy');
add_action('wp_login', 'customSessionDestroy');
function customSessionStart()
{
if (!session_id()) {
session_start();
}
}
function customSessionDestroy()
{
session_destroy();
}
When you use this code, you may get the critical issue in the WP Site Health Status. The error says that;
A PHP session was created by a session_start() function call. This interferes with REST API and loopback requests. The session should be closed by session_write_close() before making any HTTP requests.
To suppress this error, you may make this revision when you want to start a session in the WP;
session_start([
'read_and_close' => true,
]);