LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-21-2011, 12:41 PM   #1
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Rep: Reputation: 0
php session not working


Hi,

I'm new to php development. I try to learn using session. I created sess1.php with the following code:

<?php
session_start();

// makes an array
$colors=array('red', 'yellow', 'blue');
// adds it to our session
$_SESSION['color']=$colors;
$_SESSION['size']='small';
$_SESSION['shape']='round';
print "Done";
?>

sess2.php with the following code:

<?php
// this starts the session
session_start();

// echo variable from the session, we set this on our other page
echo "Our color value is ".$_SESSION['color'];
echo "Our size value is ".$_SESSION['size'];
echo "Our shape value is ".$_SESSION['shape'];
?>

I access sess1.php and then sess2.php in the same browser. The browser displayed

Our color value is Our size value is Our shape value is

which indicated that all the value set in sess1.php couldn't be accessed in sess2.php.

Can anyone let me know what's wrong?

THANKS !!!
 
Old 04-21-2011, 12:53 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You seem to be doing everything right.

Suggestions:
1. Please (re)read the PHP documentation:
http://php.net/manual/en/function.session-start.php

2. Make sure cookies are enabled in your browser.
If you're not 100% sure your browser supports cookies, try using Firefox.

3. Check the result value ("true" or "false") from session_start()>

4. Print out your SID (see the above link for details).
 
1 members found this post helpful.
Old 04-21-2011, 12:57 PM   #3
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,228
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Hi,

Welcome to the wild world of programming...

Okay, session1 and session2, you got me confused here.
Lemme see if I get your drift...you have one PHP page (one.php, let's say) with a session started up and variables and all set. What is the connection between the second page? There seems to be none...

A session is "played out" in one browser and in a set of logially belonging pages...

In page one you'd have something like this

Quote:
session_start();
$_SESSION['CoolTune'] = 'SesameStreetSong';
the next page in the same site can now open/read/access the valiable stored in the session - of that flow/site - as any other page is not part of that particular session, the variables in the session are not accessible.

Quote:
session-start();
$WhatSong = ''; // init the variable
$WhatSong = $_SESSION['CoolTune']; // fill in the data
If you need to shuttle data over, consider a database. Cookies are limited by the domain they belong to...

Hope this helped!

Thor
 
1 members found this post helpful.
Old 04-22-2011, 07:19 AM   #4
famsinyi
LQ Newbie
 
Registered: Nov 2007
Posts: 25

Original Poster
Rep: Reputation: 0
Thing works fine now. It's the permission issue on the session folder. The start_session() failed to create session. Thanks paulsm4, Thor_2.0.
 
Old 04-22-2011, 07:32 AM   #5
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,228
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
You're welcome, good luck with the project!
 
Old 04-22-2011, 10:20 AM   #6
resetreset
Senior Member
 
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,320

Rep: Reputation: 62
Quote:
Originally Posted by famsinyi View Post
It's the permission issue on the session folder.
Sorry - could you tell me what permissions you had to set?

Thanks.
 
  


Reply

Tags
php, session


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Session Fluctuate Bakbai` Programming 0 12-14-2010 12:27 PM
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 09:37 PM
PHP session is not destroyed !! prabhatsoni Programming 8 07-21-2005 11:15 AM
PHP session.gc_maxlifetime j-ray Programming 1 11-07-2004 02:45 PM
php session richard22 Programming 2 10-26-2004 06:50 AM


All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration