Can i retrieve id without using session_id() function?

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

Post Reply
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

I dont understand what you are trying to accomplish here?

To store information in a session that you can use in your application, or specific user information, you do the following:

Code: Select all

session_start();  
$_SESSION['firstname'] = $fname ;  
$_SESSION['lastname'] = $lname ;  
$_SESSION['usercity'] = $city ;  
$_SESSION['address'] = $addr ; 
To retrieve this information on another page, we do the reverse, at the top of the file, like so:

Code: Select all

session_start();  
$fname = $_SESSION['firstname'] ;  
$lname = $_SESSION['lastname'] ;  
$city = $_SESSION['usercity'] ;  
$address = $_SESSION['address'] ; 
This will retrieve the session ID from the cookie on the user's computer, and using that session ID, connect to the $_SESSION array for that particular user.

Best wishes
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

So you mean you want to use something like setcookie() instead of using session_id() yeah? Forgive me if I dont get you but its been a long day for me... lol :)

Best wishes
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Can i retrieve id without using session_id() function?

Post by phazorRise »

if you're tired of using session_id() every time then, for ease ; define it as constant.

Code: Select all

<?php
session_start();
define('PHPSESSID',session_id());
echo PHPSESSID;
?>
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

Ahh yeah, did not think about that one... lol I agree with you phaserrise but I am still not sure whether its a case of not wanting to use session_id() full stop and replace with something else or as you say just tired of using session_id() so assign a constant.

Best wishes
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Can i retrieve id without using session_id() function?

Post by phazorRise »

as far my knowledge, i don't think there's any alternative to get session ids apart from original session_id().
This method is meant to return the id.

Yes, this not alternative method anyhow but if someone want to use session id again n again then making it as constant will be good way.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

Yes, it does make it so much easier to work with sessions by making it constant.

Would setcookie() not be an alternate option instead of using session_id() altogether?

That way you assign an id string and save to cookie and recall that string value. Just a thought I had earlier thats all because I am not sure what the objective is yet.

Best wishes
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Can i retrieve id without using session_id() function?

Post by phazorRise »

I guess it's not good idea.
Sessions are much safer than cookies as sessions are stored at server. While cookies are stored at client machine and possibly can be edited also.
Someone might steal your cookie and can have access to your site as he just got your session id.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

That is so true, again just throwing ideas out there.

If by assigning a constant to a session_id() are you not decreasing the security anyway because you are assigning the session_id() to a variable that can be passed around each page which can been seen through headers anyway?

Again just elaborating on this subject :)

Best wishes
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Can i retrieve id without using session_id() function?

Post by phazorRise »

i haven't tried it anyway up till now.
But my quick answer would be, if you see most famous cms or forums out there, they store sensitive details like database host, username, password, database name etc in a file as constants. And there details are also passed on each page.

So i guess it fair enough to make our work much easier with constants.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Can i retrieve id without using session_id() function?

Post by phphelpme »

hahaha, I could not of put it better myself. :)

The rule of thumb is if it is sensitive then use a secure server anyway I guess (HTTPS)

Best wishes
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Can i retrieve id without using session_id() function?

Post by phazorRise »

yes, if you're talking one on one with a language you both know only then there are less chances someone else would listen and understand what's going on in between you. :P
Post Reply