PHP session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

br5dy
Forum Newbie
Posts: 22
Joined: Sat Jul 23, 2005 2:52 pm

PHP session

Post by br5dy »

I'm still new with sessions....haha uh...

I got everything hooked up with my site, but I want my site to remember the session. I've read many sites and some say to set a cookie with the username and password, but I'm not sure. Must I also save the session id?

Also, when I return to my site, all of my link will have ?PHPSESSID= and then the last session id. However, my current page (home) won't have it. If I click the link it does nothing.

Thanks again :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

session_start(); at the top of each page will keep the session alive once started with somtin like

$_SESSION['foo'] = 'bar';

or wahtever else you put there. no need for cookies, cookies are client side and sessions are server side
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

My question has always been, how the hell do I make a link that sets the session variable <_<
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Meaby that is because your question is not clear.

- A link, i presume you mean an url that is in a a-tag is nothing more than that. So it can't change the session variable.

- What is 'the session variable'?

I know you can start a session with session_start. I know you can change the sessionid by session_regenerate_id

I know you can store multiple variables in a session, by assignment
$_SESSION['key'] = 'value';

I know you can pass arguments in a URL, for http://example.com/test.php?id=bar

I know you can use that value to change something in the session..
$_SESSION['id'] = $_GET['id'];
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

What I was asking, although using bad phrases... was How do I convert (This was in my original thread which has been so far ignored in the past two days...) a cookie-based website into session-based, where I can do url.com/?cheez=its and have cheez = its in the session.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Tim just explained it :\
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

To convert cookie site to a session site, you just change $_COOKIE to $_SESSION, that was answered in your other thread and also by tim (or at least some one else that posted the same question), you also need to add session_start() to the top of each page.

As for ?cheez=it...

page1.php

Code: Select all

session_start();
$_SESSION['cheez']='it';
echo ("<a href='page2.php'>click</a>");
page2.php

Code: Select all

session_start();
echo ($_SESSION['cheez']);
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

So for all my links, I have to add

Code: Select all

$_SESSION['cheez']='it';
echo ("<a href='page2.php'>click</a>");
?

->Example

Code: Select all

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?del=$ver">eingang</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=news">neuigkeiten</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=rants">reklamationen</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=photos">fotos</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=poems">gedichte</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=conts">kontakte</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=links">seiten</a></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?id=scripts">scripts</a></p>
All my links are ?id=page, so I'd end up changing them all back to page.php and having a ton of session text?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

No, the example you just posted with the ?id=something where "something" is different for each link is not fit for sessions, you'd want to use mod_rewrite to clean that up, sessions are more for storing info that's going to be the same no matter what page you pass it to, for instance the user's username, or a multi part form.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Well actually, I just remembered something really obvious: I'm not using sessions for my webpage links! <_< But I do have a few links that set cookie-variables (like ?theme=color) How would I get that to work in Sessions?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

theda wrote:Well actually, I just remembered something really obvious: I'm not using sessions for my webpage links! <_< But I do have a few links that set cookie-variables (like ?theme=color) How would I get that to work in Sessions?
We've been over that, replace $_COOKIE with $_SESSION and add session_start() to the top of each page.

Here I'll even post code so we're clear:
page1.php

Code: Select all

session_start();
$color='red';
$_SESSION['color']=$color;
echo ("<a href='page.php'>click here to see what color the theme is</a>");
page2.php

Code: Select all

session_start();
echo $_SESSION['color'];
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Meh... You'll have to forgive me, I just wasn't seeing a connection as to how to do it at the time <_< *kills brain*
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

theda wrote:Meh... You'll have to forgive me, I just wasn't seeing a connection as to how to do it at the time <_< *kills brain*
eh, that's ok.. its just that we had gone over this in this thread already.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Hey, mind if we talk over messenger? I'd probably understand things and spam a little less if I was over messenger.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Talking to me?
If so:
aim: jshpro2
msn: jshpro2@hotmail.com
yahoo: jshpro2
Post Reply