sven13
sven13

Reputation: 36

PHP Session stores data for first page but not second page

I am trying to pass variables between pages using a session. The codes works when I deploy it to my server but during local development it does not. Some background information, the session broke when I moved my development over to a mac where I am using MAMP (although I was using MAMP on my windows computer as well). I've made sure my save_path is defined in the php.ini and the folder is writable. The session keeps the data for the first page, but then when I advance onto the second page the session loses all the data.

Here is my code: First page (this works):

session_start();

// retrieve Application Name and API Key
$_SESSION['appName'] = $_POST['appName'];
$_SESSION['apiKey'] = $_POST['apiKey'];
$appName = $_SESSION['appName'];
$apiKey = $_SESSION['apiKey'];

// create app and connection
$app = establishConnection($appName, $apiKey);

Second page (this is where the data is lost):

session_start();

// set default timezone
date_default_timezone_set('America/New_York');

// include openrate sdk
require_once('src/isdk.php');
include 'src/openratesdk.php';

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE | E_STRICT);

// retrieve Application Name and API Key
$appName = $_SESSION['appName'];
$apiKey = $_SESSION['apiKey'];

// create app and connection
$app = establishConnection($appName, $apiKey);

I know the code is right since it's always worked until I moved my code over to a Mac so I am sure it has something to do with the php.ini file but I cannot figure out what it is. Any help would be appreciated.

Also here is my phpinfo():

Session Support enabled
Registered save handlers    files user
Registered serializer handlers  php_serialize php php_binary

Directive   Local Value Master Value
session.auto_start  Off Off
session.cache_expire    180 180
session.cache_limiter   nocache nocache
session.cookie_domain   no value    no value
session.cookie_httponly Off Off
session.cookie_lifetime 0   0
session.cookie_path /   /
session.cookie_secure   Off Off
session.entropy_file    no value    no value
session.entropy_length  0   0
session.gc_divisor  100 100
session.gc_maxlifetime  1440    1440
session.gc_probability  1   1
session.hash_bits_per_character 4   4
session.hash_function   0   0
session.name    PHPSESSID   PHPSESSID
session.referer_check   no value    no value
session.save_handler    files   files
session.save_path   /Applications/MAMP/tmp/php  /Applications/MAMP/tmp/php
session.serialize_handler   php php
session.upload_progress.cleanup On  On
session.upload_progress.enabled On  On
session.upload_progress.freq    1%  1%
session.upload_progress.min_freq    1   1
session.upload_progress.name    PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix  upload_progress_    upload_progress_
session.use_cookies On  On
session.use_only_cookies    On  On
session.use_strict_mode Off Off
session.use_trans_sid   0   0

[Newest Edit]: When I look at my session I have noticed it creates a whole new session for the second page load instead of accessing the one already created.

Upvotes: 0

Views: 286

Answers (3)

sven13
sven13

Reputation: 36

I found a soluation. I had to specific the session id before calling session_start(). I do not fully understand why this was necessary to work because by default the session already started should just be continued so if someone could explain why this was necessary, that'd be great.

session_id('OPENRATE');
session_start();

Upvotes: 0

Riyaz
Riyaz

Reputation: 99

Am sure Session is not enabled and MAMP is different from WAMP and XAMPP. check for the permissions again. Right not in the first page also session is not working.

Upvotes: 0

prashant thakre
prashant thakre

Reputation: 5147

Are you sure /Applications/MAMP/tmp/php exist in your system.

session.save_path   /Applications/MAMP/tmp/php  /Applications/MAMP/tmp/php

Change the path which exists in your system like for windows

session.save_path   "c:/tmp" 

c:/tmp is just and example like me having folder tmp named inside the C drive.

the same like windows you need to check in Mac a proper directory which exist.

Upvotes: 1

Related Questions