Solved

PHP and sessions

Posted on 2011-09-26
5
290 Views
Last Modified: 2012-06-27
I'm trying to save a url in a session variable so when I go to a new page, that url is available.

Here's what I'm doing. Note that this is an ExpressionEngine template and PHP is allowed and parsed before any javascript is sent to the browser.
Any session storage should take place when the PHP is parsed.

// if I place session_start() here, I get Session already started error
var REGISTER_URL            = '{site_url_https}register/';
function logon_or_signup() {
<?php 
  echo "// setting session info"."\n";
  $_SESSION['returnURL'] = "{site_url_https}/schedule";
  // it echos out perfectly at this point
  echo "//".$_SESSION['returnURL']."\n";
?>
  window.location = REGISTER_URL;
}

Open in new window


Now in the REGISTER_URL page:
<?php 
  session_start(); // don't get Session already started error here.
  echo "//".$_SESSION['returnURL']."\n"; // Getting undefined Index returnURL here
  if(isset($_SESSION['returnURL'])) {          
    echo "         <input type=\"hidden\" name=\"returnURL\" id=\"returnURL\" value=\"".$_SESSION['returnURL']."\" />";
  } else {          
    echo "         <input type=\"hidden\" name=\"returnURL\" id=\"returnURL\" value=\"/\" />";
  }
?>          

Open in new window

It just isn't working between the pages. Session values from phpinfo():
Session Support 		enabled
Registered save handlers 	files user mm
Registered serializer handlers 	php php_binary

Directive			Local Value	Master Value
session.auto_start		Off		Off
session.bug_compat_42		Off		Off
session.bug_compat_warn		On		On
session.cache_expire		180		180
session.cache_limiter		nocache		nocache
session.cookie_domain		no value	no value
session.cookie_httponly		On		On
session.cookie_lifetime		0		0
session.cookie_path		/		/
session.cookie_secure		Off		Off
session.entropy_file		/dev/urandom	/dev/urandom
session.entropy_length		16		16
session.gc_divisor		1000		1000
session.gc_maxlifetime		1440		1440
session.gc_probability		1		1
session.hash_bits_per_character	5		5
session.hash_function		1		1
session.name			PHPSESSID	PHPSESSID
session.referer_check		no value	no value
session.save_handler		files		files
session.save_path		/var/lib/php5	/var/lib/php5
session.serialize_handler	php		php
session.use_cookies		On		On
session.use_only_cookies	On		On
session.use_trans_sid		0		0

Open in new window


Any ideas on how to fix this?
0
Comment
Question by:EddieShipman
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 500 total points
ID: 36601437
see if anything is being written to the cookie in your browser and to the session file under your session.save_path location.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 36601475
If I am able to check the value when it is written, wouldn't that be enough? I can't tell if anything is being written in the cookie because all it has is the ID. I don't see any changes to the dir in the savepath in the file with the same sessionid.
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36601510
That is all that is supposed to be in the cookie, and I was trying to see if anything was getting set up for the session at all.
I would also like to know if you are getting the same session id for both pages or if it is being changed in between pages?
You can use session_id() to figure out what it is.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 36601558
I got it figure out. I moved the code from the javascript template to the HTML template and it worked perfectly.
It worked before with the code in the javascript template but what the worry? It works now...

Thanks.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36601583
"/var/lib/php5" for "session.save_path" implies a Linux or Unix system.  Is that what you running on?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to change the link of an image using md5 in php ? 3 47
XML extra information 8 30
Wordpress Query 5 27
Reference key in foreach loop 4 22
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 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