Solved

Why my hosting session is not accessible in other pages?

Posted on 2014-02-20
8
585 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

776 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