LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-04-2006, 10:06 AM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Question PHP Code Inquiry


Hey Gang,

I've got yet another PHP question for you all. I am thinking this should be possible, but I have no clue how to implement it.

Let's say you have a dynamic page that stores/passes user selections on the page via variables through the address bar: (to be used in GETS later)

page.php?sortby=iid&sorder=0

What if I want to add another user selection option like a drop down box that allows further customization of the page display:

page.php?sortby=iid&sorder=0&rpp=10

When I'm building the links for the drop down box, I don't wish to have to hard code all the previous selections in the address bar just to add another one on. I would like to simply build the link to say put &rpp=10 on the end of whatever is already on the address bar, so it doesn't matter if the selection is made at this point:


page.php?sortby=iid&sorder=0 + &rpp=10

or this point

page.php?sortby=iid&sorder=0&cpn=3 + &rpp=10


As it is right now I'm saying http://?sortby=$sortby&sorder=$sorder&rpp=$rpp
but if i wanted to say: http://sortby=$sortby&sorder=$sorder&cpn=$cpn&rpp=$rpp
I would have to go in and physically change each link, and that sucks.

Is there anyway to say something like: http://?-all previous after ? stuff-&rpp=$rpp
(Without knowing specifically each time how much previous stuff there will be)

I hope I phrased this clearly enough.

Thanks for any assistance on this, I know i'm really limiting myself hard coding these links each time, when I simply wish to append to an existing string.

-Karl
 
Old 08-04-2006, 10:09 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Just use a $_SESSION variable to store this string and append whatever options are there to the existing string.
 
Old 08-04-2006, 10:46 AM   #3
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Session Variable

A session variable does seem like a much better alternative to trying to pass this information via the address bar and $_GETS. I am very new to sessions. Is there any danger of session variables I create interfering with existing session variables of coincidentally the same name in other unrelated scripts running on the same server?

Example: say my script stores the current page number for a thread in a session called:

$_SESSION['pgnum'] = 1


and another programmer does the same thing with his own unrelated script, $_SESSION['pgnum'] = 3

I guess I am unsure of the scope and protection of session variables as they are being stored on the server.

Thanks.
 
Old 08-04-2006, 10:50 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
No. Sessions in PHP are safe and session vars apply only for *that* particular user for that particular session. Especially in the later versions of PHP 4 (4.4+) and PHP 5. Safer than query strings which can be easily read because they're unencrypted. It also depends on PHP.ini settings, but generally using sessions is a fairly safe bet.

Don't forget to call session_start() in the beginning of every PHP file using sessions in your app though or it won't work.

Last edited by vharishankar; 08-04-2006 at 10:53 AM.
 
Old 08-04-2006, 11:04 AM   #5
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Sessions definitely seem like a great tool, and a safer alternative. As far as internal protections though, I don't need to worry about my Session variables writing over another programmers Session variables on another project. I guess I'm still kind of viewing them like "Global" variables. Your're saying the there is no way my session variables could over-write another programmer's session variables, as the server will create a unique name for each one?

For instance my script runs and says:

<?php
session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

and programmer 2 unrelated script says

<?php
session_start();
$_SESSION['views'] = 2; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

We are in no danger of over-writing each other's session variables on the server?

Also, how long does Session data stay on the server if it's not explicitly destroyed with: session_destroy();

Last edited by joelhop; 08-04-2006 at 11:10 AM.
 
Old 08-04-2006, 11:13 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Read the comments here: http://in.php.net/session_destroy
 
Old 08-04-2006, 11:28 AM   #7
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
I think I understand the scope of a session variable now. It is very temporary, it only exists for a single visit to the page, unlike a traditional cookie which would be used to retrieve data from previous visits. A session is a server side way to move accumulated data from a single visit, (to the checkout) so to speak. However once the script finishes (the user leaves the site/closes the browser/logs out) the session variables are destroyed. Therefore, it is not like other developers have a bunch of session variables just hanging out waiting to be over-written, as they are quickly and uniquely created and destroyed with each new user visit.

it's not like each user gets session[views] and if 2 users were on at the same time their alternating session[views] the temp unique id cookie takes care of this so to speak.
 
Old 08-04-2006, 01:01 PM   #8
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
This site really helped me to understand how this whole thing works:

http://devzone.zend.com/node/view/id/646
 
Old 08-04-2006, 08:38 PM   #9
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Note that sessions can be as temporary as the session cookie set at the user end. The difference between session cookie and normal cookies is that traditionally normal cookies contain all the cookie variables while session cookie contains only the session ID and the actual variables are stored in the server end.

Also sessions can exist without cookies by passing the session ID in the URL but that's generally not a very ideal way and is used only when the user has disabled storage of cookies at the client end.

Also note that session behaviour can be controlled using PHP.ini

Last edited by vharishankar; 08-04-2006 at 08:39 PM.
 
  


Reply



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
need help with this php code lakivel Programming 1 02-24-2006 12:06 PM
merge ASP code with PHP code.. possible ?? ALInux Programming 7 12-30-2005 08:40 AM
Help With PHP Code windisch Programming 4 09-09-2005 07:37 AM
PHP something wrong with the code :( Alexander.s Programming 2 04-22-2005 04:27 AM
php code lynger Linux - Software 0 01-07-2004 02:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 PM.

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
Open Source Consulting | Domain Registration