Solved

Why my hosting session is not accessible in other pages?

Posted on 2014-02-20
8
581 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
  • 5
  • 3
8 Comments
 
LVL 82

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
 
LVL 82

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 36

Author Comment

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now