Solved

Why my hosting session is not accessible in other pages?

Posted on 2014-02-20
8
595 Views
Last Modified: 2014-02-21
I have bought new hosting package (linux, php) for my product site. In this hosting ,I'm facing session sharing/accessing problem in sub pages/ folders. How to resolve this?

I have tested single session in same page, it works.. but when I access the session variable in other pages , it shows empty.  What could be the problem ? Please advise.

This works fine (same page)
<?php
session_start();

print_r ($_REQUEST);

 if($_REQUEST['usr']=="ABC" && $_REQUEST['pswd']=="123"){

 $_SESSION['usr'] = "ABC";
 $_SESSION['pswd'] = "123";
 
 print_r ($_SESSION);
 exit;
 
 header("Location: content.php");
 
 }
 else{
 header("Location: niceform.php");
 }
?>

Open in new window


Here it is not working,

<?php
session_start();

print_r ($_SESSION);

if(!isset($_SESSION['usr']) || !isset($_SESSION['pswd'])){
 header("Location: niceform.php");
} 
 include 'logoff.html';

?>

Open in new window

0
Comment
Question by:Loganathan Natarajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39876061
I modified your code a bit and you can see it work here:  http://www.dibsplace.info/index.php  When you're 'logged in' it goes to http://www.dibsplace.info/content.php and when you're logged out it goes to http://www.dibsplace.info/niceform.php

index.php
<?php
session_start();

//print_r ($_REQUEST);

if($_REQUEST['usr']=="ABC" && $_REQUEST['pswd']=="123"){

 $_SESSION['usr'] = "ABC";
 $_SESSION['pswd'] = "123";
 
 //print_r ($_SESSION);
 //exit;
 
 header("Location: content.php");
 }
 
 else{
 $_SESSION['usr'] = "";
 $_SESSION['pswd'] = "";
 
 header("Location: niceform.php");
 }
?>

Open in new window


content.php
<?php
session_start();

print_r ($_SESSION);

if(!isset($_SESSION['usr']) || !isset($_SESSION['pswd'])){
 header("Location: niceform.php");
} 

include 'logoff.html';

?>

Open in new window


logoff.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>logoff.html</title>
</head>
<body>
<h1>logoff.html</h1>
<p>But we're really logged in?</p>
<a href="index.php">index.php</a><br />
or login <a href="index.php?usr=ABC&pswd=123">index.php?usr=ABC&pswd=123</a><br />

or logout <a href="index.php?usr=v&pswd=9">index.php?usr=v&pswd=9</a>

</body>
</html>

Open in new window


niceform.php
<?php
session_start();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>niceform.php</title>
</head>
<body>
niceform.php<br />
We're logged out.<br />

<a href="index.php">index.php</a><br />
or login <a href="index.php?usr=ABC&pswd=123">index.php?usr=ABC&pswd=123</a><br />
or logout <a href="index.php?usr=v&pswd=9">index.php?usr=v&pswd=9</a><br />

<?php print_r ($_SESSION);
 ?>
</body>
</html>

Open in new window

0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 39876167
Thanks, Let me check
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 39876232
@ Dave Baldwin

I checked it looks empty again. Check the attachment.

I think session is not working the hosting.  Can you tell me how to inform them to fix?
err1.png
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39876274
Try this to see if it is generating a session_id.
<?php
session_start();

$sessid = session_id();

echo $sessid;
?>

Open in new window

0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 39876324
yes , it is generating,
75c7b35536c97069ec7ce6eec9e696fbArray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39876347
Does it generate the same number if you refresh the page several times?  Or does it change?  Do you have cookies enabled in your browser?
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 39876356
Thanks Dave for your support so far.

I just found solution that session_path is not defined in the hosting and configured now
. Now it is working fine.

75c7b35536c97069ec7ce6eec9e696fbArray ( [Yii.CCaptchaAction.3c85ef20.site.captcha] => kanuqij [Yii.CCaptchaAction.3c85ef20.site.captchacount] => 2 [products] => [92d77c121165b256e0981213cac80aa3id] => 1 [userid] => 1 [92d77c121165b256e0981213cac80aa3userType] => Administrator [usr] => ABC [pswd] => 123 )
logoff.html

But we're really logged in?
index.php
or login index.php?usr=ABC&pswd=123
or logout index.php?usr=v&pswd=9

Open in new window

0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 39876360
This helped to test my session on hosting. Thanks
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question