Working on mediawiki project facing the issue of frequent logouts where user when login and try some edit and update operation then after spending few minutes users gets automatically signout and redirects it to login page.
I have tried the following approaches added in LocalSettings.php but none of them works-
APPROACH-1
$wgMainCacheType = CACHE_ACCEL; $wgSessionCacheType = CACHE_DB;
APPROACH-2
$wgSessionTimeout = 604800; $wgCachePages = false;
APPROACH-3
ini_set('session.gc_maxlifetime', 604800); session_set_cookie_params(604800); session_start();
APPROACH-4
$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', time() ) ); header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 3600 ) . ' GMT' ); header( 'Cache-Control: public, max-age=3600' );
APPROACH-5
$wgParserCacheType = CACHE_NONE; $wgCachePages = false;
I have also used one approach where i have go inside the docker and commented out the following code in mediawiki/includes/Setup.php which is somehow works but triggered one more issue.
if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
// If session.auto_start is there, we can't touch session name
if ( $wgPHPSessionHandling !== 'disable' && !wfIniGetBool( 'session.auto_start' ) ){
session_name($wgSessionName ?: $wgCookiePrefix . '_session');
HeaderCallback::warnIfHeadersSent();
}
//Create the SessionManager singleton and set up our session handler,
// unless we're specifically asked not to.
if ( !defined( 'MW_NO_SESSION_HANDLER' ) ) {
MediaWiki\Session\PHPSessionHandler::install(
MediaWiki\Session\SessionManager::singleton()
);
}
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
// Initialize the session
try {
$session = MediaWiki\Session\SessionManager::getGlobalSession();
} catch ( MediaWiki\Session\SessionOverflowException $ex ) {
// The exception is because the request had multiple possible
// sessions tied for top priority. Report this to the user.
$list = [];
foreach ( $ex->getSessionInfos() as $info ) {
$list[] = $info->getProvider()->describe( $contLang );
}
$list = $contLang->listToText( $list );
throw new HttpError( 400,
Message::newFromKey( 'sessionmanager-tie', $list )->inLanguage( $contLang )
);
}
unset( $contLang );
if ( $session->isPersistent() ) {
$wgInitialSessionId = $session->getSessionId();
}
$session->renew();
if ( MediaWiki\Session\PHPSessionHandler::isEnabled() &&
( $session->isPersistent() || $session->shouldRememberUser() ) &&
session_id() !== $session->getId()
) {
// Start the PHP-session for backwards compatibility
if ( session_id() !== '' ) {
wfDebugLog( 'session', 'PHP session {old_id} was already started, changing to {new_id}', 'all', [
'old_id' => session_id(),
'new_id' => $session->getId(),
] );
session_write_close();
}
session_id( $session->getId() );
session_start();
}
unset( $session );
} else {
// Even if we didn't set up a global Session, still install our session
// handler unless specifically requested not to.
if ( !defined( 'MW_NO_SESSION_HANDLER' ) ) {
MediaWiki\Session\PHPSessionHandler::install(
MediaWiki\Session\SessionManager::singleton()
);
}
}
After removing the above code frequent logouts problem is solved in mediawiki project.
after removing the above code inside Setup.php
Can anyone assist here?
Related
I am setting up a new Wordpress site that utilizes the Spoonacular API.
I want to use a recursive function to render 50 items from the API, but I cannot figure out why my code is not calling the function again.
The result I am expecting to get, is that the 'report.txt' file that is created will display a constantly updating number up to the specified per_page number '50'.
The 'report.txt' file only displays the default number '1' instead of incrementing.
I am not sure if the function is being called again or not.
Any help is greatly appreciated. Thank you.
functions.php
<?php
$apiKey = '123456789';
function get_ingredients_from_api() {
$file = get_stylesheet_directory() . '/report.txt';
$current_ingredient = ( ! empty( $_POST[ 'current_ingredient' ] ) ) ? $_POST[ 'current_ingredient' ] : 1 ;
$ingredients = [];
$results = wp_remote_retrieve_body(
wp_remote_get('https://api.spoonacular.com/food/ingredients/autocomplete?query=appl&number=' . $current_ingredient . '&per_page=50&apiKey=' . $apiKey)
);
file_put_contents( $file, "Current Ingredient: " . $current_ingredient. "\n\n", FILE_APPEND);
$results = json_decode($results);
if ( ! is_array( $results ) || empty( $results ) ) {
return false;
}
$current_ingredient = $current_ingredient + 1;
wp_remote_post( admin_url('admin-ajax.php?action=get_ingredients_from_api'), [
'blocking' => false,
'sslverify' => false,
'body' => [
'current_ingredient' => $current_ingredient
]
]);
}
add_action('wp_ajax_nopriv_get_ingredients_from_api', 'get_ingredients_from_api');
add_action('wp_ajax_get_ingredients_from_api', 'get_ingredients_from_api');
?>
I realized that commenting out or removing the if statement from the code allows the function to run perfectly. I'm not sure how to keep the if statement and have the function run yet.
I am trying to get user detail out side wordpress file (But same server) and for that I am using this code
<?php
define( 'WP_USE_THEMES', false ); // Do not use the theme files
define( 'COOKIE_DOMAIN', false ); // Do not append verify the domain to the cookie
define( 'DISABLE_WP_CRON', true ); // We don't want extra things running...
//$_SERVER['HTTP_HOST'] = ""; // For multi-site ONLY. Provide the
// URL/blog you want to auth to.
// Path (absolute or relative) to where your WP core is running
require("/var/www/yourdomain.com/htdocs/wp-load.php");
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds = array();
// If you're not logged in, you should display a form or something
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = "";
$creds['user_password'] = "";
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
}
if ( !is_wp_error( $user ) ) {
// Success! We're logged in! Now let's test against EDD's purchase of my "service."
if ( edd_has_user_purchased( $user->ID, '294', NULL ) ) {
echo "Purchased the Services and is active.";
} else {
echo "Not Purchased";
}
}
?>
but it doesn't worked, I am creating custom dashboard out wp, which user wp info, as back-end. So please tell me what wrong am i doing? Any help is highly appreciated.
Since you are using easy-digital-downloads be sure it was included
if ( !function_exists( 'edd_has_user_purchased' ) ) {
require_once 'path-to-plugin/user-functions.php';
}
In your if ( !is_wp_error( $user ) ) statement you can use
echo "<p>ID: ".$user->ID;
echo "<p>Name: ".$user->data->display_name;
echo "<p>Login: ".$user->data->user_login;
echo "<p>Email: ".$user->data->user_email;
echo "<p>URL: ".$user->data->user_url;
echo "<p>Registered: ".$user->data->user_registered;
to show user wp info.
Try
print_r($user);
to overview all accessible fields
I am using wp_signon from inside a plugin and inside a shortcode function to login a user. To clarify, the short code is being called from a page content. The wp_signon function successfully returns the user info. But is_user_logged_in() is returning false. Why it's happening or how can I set is_user_logged_in() true so that user stays login?
ob_start();
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
ob_get_clean();
if ( is_wp_error($user) ) {
$err = $user->get_error_message();
} else {
$_SESSION['fc_user']=$user->ID;
var_dump($user); // gives the user info
var_dump(is_user_logged_in()); // gives false
}
The function wp_signon
sends headers to the page. It must be run before any content is returned.
So running it inside a shortcode (like you specified in your comment) is too late: headers have already been sent.
In order to achieve what you want you should divide your code in two parts, to be run at different time, something like this:
add_action( 'after_setup_theme', function() {
// run this only on the page you want
if( !is_page('the_page_you_want') )
return;
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
if ( is_wp_error($user) ) {
$GLOBALS['user_error_message'] = $user->get_error_message();
} else {
$_SESSION['fc_user'] = $user->ID;
}
} );
add_shortcode( 'your_shortcode', function( $atts ) {
if( is_user_logged_in() ) {
// get user info
return print_r( get_userdata(get_current_user_id()), 1 );
} elseif( isset($GLOBALS['user_error_message']) ) {
// show error
return print_r( $GLOBALS['user_error_message'], 1 );
}
} );
I just ran into a similar issue with an MVC Wordpress plugin.
For me the solution was to include
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/pluggable.php');
to have access to the core function via a plugin.
This is a follow-up question from here.
The code is working great, it randoms the link to a blog post. The downside is that I may fall into the same post twice or too often.
header("Location: ".$posts[array_rand ($posts)][1]);
I need it to not fall on the same post more than once every 20 minutes, if runs out of posts then say: "Out of options. Come back in 20 minutes.". I tried doing it with cookies like so:
$rlink = $posts[array_rand ($posts)][1];
setcookie("rlink", "$rlink", time()+1200);
if ($rlink == $_COOKIE["rlink"]) {
header('Location: http://localhost/randommyblog.php');
} else
{
header("Location: ".$rlink);
}
It might be obvious that the problem here is that I'm replacing the cookie "rlink" every time, rendering the previous one useless.
Little help, please?
Try something like this, worked when I tested it as is:
$posts = array("hello", "world", "it's", "me" );
$len_posts = count( $posts );
$set_indices = #$_COOKIE['rlink'];
$rand = mt_rand( 0, $len_posts - 1 ); //Select a random index from the post
if( !empty( $set_indices ) )
{
$set_indices = array_map( "intval", explode( ",", $set_indices ) );
$len_indices = count( $set_indices );
if( $len_indices >= $len_posts )
{
die("no posts for you");
}
else
{
while( in_array( $rand, $set_indices, TRUE ) ) //Calculate a new index that has not been shown.
{
$rand = mt_rand( 0, $len_posts - 1 );
}
}
}
else
{
$set_indices = array();
}
array_push( $set_indices, $rand );
setcookie( "rlink", implode( ",", $set_indices ), time()+1200 ); //Set cookie of the shown indices like "3,0,1" for example.
echo $posts[$rand];
In my theme, there's custom page for the login. Login function at functions.php is like this
function log_in($username, $password) {
$user = parse_user($username);
$username = $username;
$password = $password;
if(isEmptyString($username)) return new WP_Error('username', 'required');
if(isEmptyString($password)) return new WP_Error('password', "required");
if(!wp_check_password( $password, $user->user_pass ) ) return new WP_Error('wrong_password', "wrong");
wp_set_auth_cookie($user->ID, $remember);
wp_login($username, $password);
redirect_profile();
}
function parse_user($info = null, $return = 'object') {
if ( is_null( $info ) ) {
global $current_user;
if ( empty( $current_user->ID ) ) return null;
$info = get_userdata( $current_user->ID );
}
elseif ( empty( $info ) ) {
return null;
}
if( $return == 'ID' ) {
if ( is_object( $info ) ) return $info->ID;
if ( is_numeric( $info ) ) return $info;
}
elseif( $return == 'object' ) {
if ( is_object( $info ) && $info->ID) return $info;
if ( is_object( $info )) return get_userdata( $info->ID );
if ( is_numeric( $info ) ) return get_userdata( $info );
if ( is_string( $info ) ) return get_userdatabylogin( $info );
}
else {
return null;
}
}
I want to add remember me checkbox for user to logged in all the time until they logout. How can i add this ? Please kindly help me out. Thank you.
"remember me" buttons are generally just a simple tweak to the cookie settings internally. Instead of a session cookie that gets deleted when the browser is exitted, a "remember me" login cookie gets some future expiration point (a day, a month, a year, etc...) so it'll persist after the browser's closed.
In pseudo-code, you'd have:
if (form_value('remember_me') == 'yes) {
set_long_term_cookie();
} else {
set_session_cookie();
}
"Add a login form on your WordPress Theme" (including remember me functionality):
http://www.wprecipes.com/add-a-login-form-on-your-wordpress-theme
Also: http://www.problogdesign.com/how-to/how-to-create-a-wordpress-login-form-overlay/
etc...