0

I have a formal code, but the session variable can't be passed to another page. So here is my sample code. However, the result of this code is 0.:

page1 code:

<?php
session_start();
$_SESSION['try'] = 5;
header('Location: page2.php');
?>

page2 code:

<?php
session_start();
$test = $_SESSION['try'];
echo $test;
?>
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
user2596579
  • 7
  • 1
  • 4

1 Answers1

0

From the manual:

session_start() creates a session or resumes the current 
one based on a session identifier passed via a GET or POST request, 
or passed via a cookie.

pay attention to the last two lines of code:

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    Also from [manual](http://www.php.net/manual/en/session.configuration.php#ini.session.use-only-cookies) - session.use_only_cookies specifies whether the module will only use cookies to store the session id on the client side. Enabling this setting prevents attacks involved passing session ids in URLs. This setting was added in PHP 4.3.0. Defaults to 1 (enabled) since PHP 5.3.0. – Orangepill Jul 18 '13 at 17:53
  • Thank you. I tested the code. After I clicked "page 2", the result is still 0, which means no value of the session variable passed to the 2nd page. – user2596579 Jul 18 '13 at 21:22
  • in page2 do `var_dump($_SESSION);` and check if anything has passed at all. – Nir Alfasi Jul 18 '13 at 21:25
  • Thank you, but the result is: array(0) { }. – user2596579 Jul 18 '13 at 21:40
  • Run `phpinfo()` and check the session settings. Also, check this out: http://stackoverflow.com/a/8977032/1057429 – Nir Alfasi Jul 18 '13 at 21:46
  • Where should I run phpinfo()? – user2596579 Jul 18 '13 at 21:52
  • create a new php file and put there `` – Nir Alfasi Jul 18 '13 at 21:55
  • here are some of the session settings session Session Support enabled Registered save handlers files user sqlite Registered serializer handlers php php_binary wddx Directive Local Value Master Value session.auto_start Off Off session.bug_compat_42 Off Off session.bug_compat_warn 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 /dev/urandom /dev/urandom – user2596579 Jul 18 '13 at 22:07
  • what should I pay attention to in the settings – user2596579 Jul 18 '13 at 22:08
  • look for `session.save_path` and make sure this folder has write permissions – Nir Alfasi Jul 18 '13 at 22:14
  • sorry to give you so much work. in my setting, the session.save_path is : /var/php_sessions, how can I know the folder's permissions – user2596579 Jul 18 '13 at 22:20
  • run from command prompt (ssh): `ls -l /var/ | grep php_sessions` – Nir Alfasi Jul 18 '13 at 22:36
  • The command doesn't work: C:\users\xxx\.ssh> ls -l /var/ | grep php_sessions 'ls' is not recognized as an internal or external command, operable file, or batch program – user2596579 Jul 18 '13 at 22:47
  • It looks like you're working on a windows machine and the path is a unix style path... Try creating a folder for the sessions to be stored and update your `php.ini` file with that path. – Nir Alfasi Jul 19 '13 at 03:46
  • Thanks. I am using i-page remote server. That's probably why the path is a unix style. What can I do in this case? – user2596579 Jul 19 '13 at 15:42
  • it solved, I contact with I-page, they set up the session permission for me, my code works now. – user2596579 Jul 19 '13 at 16:40
  • Thank you everyone. Thank you alfasin. I thought I have hope to solve the problem when you give me hint every time. The feeling is good. Thanks – user2596579 Jul 19 '13 at 16:42
  • another question: I want to pass parameter now between 2 pages, in page1: .......... $problem = "correct"; header('Location:trynew2.php?problem=$problem'); in page 2: ... $problem = $_GET['problem']; echo $problem; ........... but couldn't display "correct" – user2596579 Jul 19 '13 at 17:42