I want to add session and retrieve value from it in wordpress. I am using subdomain for my website. I have added code in header.php file of a theme to retrive a subdomain and put it in session. I have started session and put the value in it. When I use subdomain in url session retrives session value but without subdomain name, it doesn't works, even session have that value. Following are my code:
$url = $_SERVER['SERVER_NAME'];
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomain = $host[0];
$wp_session = WP_Session::get_instance();
if(isset($wp_session['user_subdomain']))
{
?>
<script>alert('<?php echo "Check first session Set ".$wp_session['user_subdomain']; ?>');</script>
<?php
}
else
{
?>
<script>alert('<?php echo "Check first session Not Set "; ?>');</script>
<?php
}
if($wp_session['user_subdomain']=="test"){
?>
<script>alert('<?php echo "session ".$wp_session['user_subdomain']; ?>');</script>
<?php
$red_url=$wp_session['user_subdomain'].".domain.com";
?>
<script>alert('<?php echo "RED ".$red_url; ?>');</script>
<?php
header("location:$red_url");
}
else if($subdomain=="test"){
$red_url=$_SERVER['SERVER_NAME'];
$wp_session['user_subdomain']=$subdomain;
?>
<script>alert('<?php echo "get ".$subdomain; ?>');</script>
<script>alert('<?php echo "check set session ".$wp_session['user_subdomain']; ?>');</script>
<?php
header("location:$red_url");
}
I have added '_SESSION' in wp_unregister_GLOBALS function, I found it somewhere on google.
You have to set session id on subdomain too.
Try to add this line
ini_set('session.cookie_domain', '.example.com' );
More info
We give priority to set session. The following code will works fine.
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
Related
I'm redirecting to page by link "welcome.php?language=english".
But when I place function 'confirm_patient_logged_in()' below Session declaration it's showing "Website not found". while it redirects to 'login.php' when this function is placed above Session declaration.
Can anyone tell what is reason behind this?
Welcome.php
<?php require_once("../../includes/session.php"); ?>
<?php confirm_patient_logged_in(); ?> //WORKING HERE
<?php
if(isset($_GET["language"])){
if($_GET["language"] == "english"){
$_SESSION["language"] = "english";
}else{
$_SESSION["language"] = "hindi";
}
}
?>
<?php confirm_patient_logged_in(); ?> // NOT WORKING HERE
Function:
function confirm_patient_logged_in(){
if(!patient_logged_in()){
redirect_to("login.php");
}
}
function redirect_to($new_location){
header("Location: " . $new_location);
exit;
}
patient_logged_in() checks if user is logged-in or not
It seems like you forgot to add session_start(); before using $_SESSION variable:
<?php
require_once("../../includes/session.php");
session_start();
if(isset($_GET["language"])){
if($_GET["language"] == "english"){
$_SESSION["language"] = "english";
}else{
$_SESSION["language"] = "hindi";
}
}
confirm_patient_logged_in(); // SHOULD WORK
?>
So it the missing session_start(); was causing an error but you were unable to see the error probably display_error ini php.ini is disabled and you were getting Website not found notification on your browser.
What's the correct way of passing variable using PHP cookie. I can't seem to get it to work? I keep getting "FAILED!"
Here's my code:
On 1st page:
$crpid['ONE']="PAGE1";
$crpid['TWO']="PAGE2";
$crpid['THREE']="PAGE3";
$crp_id = $_SERVER["REDIRECT_URIPART"];
$crp_value = $crpid[$crp_id];
session_start();
setcookie('crpid', $crp_value, time()+3600, "/");
On 2nd page:
if(!isset($_COOKIE['crpid']) && $_COOKIE['crpid']==''){
echo "FAILED!";
}
else{
echo "Cookie ".$_COOKIE['crpid']." is set!";
}
Im creating an website where i am checking for login and redirecting the user to the index page, if his login was successful i want him to see something else instead of the login button
i have followed this approach for my query
<?php
if(!isset($_SESSION['uid']))
{
?>
<span class="Login">Login</span>
<?php
}
else if(isset($_SESSION['uid']))
{
?>
<span>Post</span>
<?php
}
?>
it doesn't seem to work quite the way i want. The 'Login' span is always visible, it would seem that the $_SESSION['uid'] is not being set, but that is not the case. To be honest i don't even know if this is the correct way of doing this
You need to put session_start(); in each page that need to access the session data before accessing (or creating) any session data.
See: Session Manuel
<?php
session_start();
$linkPage = 'login.php';
$linkName = 'Login';
if(isset($_SESSION['uid'])) {
$linkPage = 'postThread.php';
$linkName = 'Post';
}
?>
<span class="link"><?php echo $linkName; ?></span>
Why page can not get the $temp_kt value? I tested $_SESSION['temp_kt'] and $_ENV['temp_kt'], neither worked.
<?php
$temp_kt=0;
if(isset($_POST['db']))
{
if($_POST['db']=="feedback")
{
global $temp_kt;
$temp_kt=$_POST['temp_kt'];
}
exit();
}
if(isset($_GET['q']))
{
echo "temp_kt=".$temp_kt;
}
?>
You have exit in if(isset($_POST['db'])) which mean that you can't have both if statements. If you want to save that value in session you should use code like this:
<?php
session_start();
if (isset($_POST['db'])) {
if ($_POST['db']=="feedback") {
$_SESSION['temp_kt'] = $_POST['temp_kt'];
}
exit();
}
if (isset($_GET['q'])) {
echo "temp_kt=" . $_SESSION['temp_kt'];
}
?>
session_start function will enable session for you, you need it at the begining when you set and get session values (it will send cookie to the browser - using header - so you can't have any echo before).
I created two files
1.php
2.php
which are in the same folder(i am using xampp).
In 1.php used session_start() and also used $_session['name']=abc. Then i opened 2.php to check whether session was created or not
2.php:
<?php
if(isset($_session['name'])){
echo $_session['name'];
}
else{
echo "no session found!!";
}
?>
and it keeps on saying "no session found!!"
Plz help ...
I searched a few sites n they say that by default d session is for whole folder containing
d script and session_set_cookie_params($lifetime,'/') (where $lifetime=60*60) is also nt helping.
On d other hand if at d end of1.php i use require("2.php") then abc is displayed.
What you have done is right in 1.php,
however, 2.php must start the session before using it.
2.php
<?php
session_start();
if(isset($_SESSION['name'])) {
echo $_SESSION['name'];
}
else{
echo "no session found!!";
}
?>
You're missing session_start() at the top of your 2.php file which is needed to access $_SESSION variables.
<?php
session_start(); // missing
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else
{
echo "no session found!!";
}
?>
You need to call session_start(); again at the top of every page where you want to access $_SESSION variables, not only on the page where you want to initiate the session.
<?php
session_start();
if(isset($_SESSION['name'])){
echo $_SESSION['name'];
}else{
echo "no session found!!";
}
?>