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/
Related
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);
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.
I have an issue with a WordPress session. I have a file 'test.php' that is used post a variable to a WordPress site. It has the condition: "if the session variable is set then the user can access the whole WordPress site, and if the session variable is not set then user can't access the site".
When I post the variable to the WordPress site using test.php the homepage works fine, but when I access inner pages like 'xyz.com/contact' I get an error Not Access which means that the session variable was cleared on the next page.
Here is the test.php file:
<form action="wordpress-site-link" method="POST">
<input type="submit" name="var" value="go"/>
</form>
In the file themes/theme-name/header.php I wrote this code:
session_start();
if(isset($_SESSION['var'])) {
echo 'Welcome';
} else if(isset($_POST['var'])) {
$_SESSION['var'] = $_POST['var'];
} else {
echo 'No access...';
exit;
}
Just hook a function on "init" in your functions.php like this :
function ur_theme_start_session()
{
if (!session_id())
session_start();
}
add_action("init", "ur_theme_start_session", 1);
Then u can use your session variables.
I hope that help u.
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.