LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-07-2006, 11:55 AM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Question Php Coding Question


Hey Everyone,

I am just getting the hang of session variables in PHP. I understand the purpose of session variables, I understand their scope, and I understand the structure of setting a session variable"

$_SESSION['value'] = 10;

However, I would like to being to use them more dynamically. I have a function that builds links dynamically using $_GET to receive variables being passed through the URL.

So the link snippit looks like this:

<a href=\"?cpn=$x\"></a>

(Current page number where x is 1,2,3,4,5 depending on which link built in the loop)

I don't want to pass the variables through the URL anymore, but I would still like the each individual link to change the value of that variable, just now a Session variable.

I am just wondering what the best way to approach this is? Can you dynamically build links that say:

for ($x=0; $x<5; $x++)
{
<a href=\"?" onclick->set Session variable to $x></a>
}

I guess I just don't understand how you can tell a link to set a Session variable when a user clicks on it?

Thanks!

Last edited by joelhop; 08-07-2006 at 11:57 AM.
 
Old 08-07-2006, 12:30 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
the Session variables lives in the server, so you cant change them from a JS of a link. What you can do is make the link fixed, ie:
<a href="yoursite.com?cpn=next">Next</a>

So in your PHP script, when you see that the link has a next value, you take your $_SESSION['currentPage'] variable, add one, and display the proper page. (you can do the same with a 'previous' link).

Hope this is usefull!
Cheers!
 
Old 08-07-2006, 12:30 PM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
(warning: the defining characteristic of "reload", as I use it throughout this post, is that in un-renders what the viewer is currently looking at, most often as it's switching to a logically different page).

Well, as PHP lives on the server side, to modify the session variable, you either have to push the variable into client-space (do some of your stuff in javascript and/or ajax); or, have the client talk to the server.

AFAIK (but I may be wrong), clients only talk to the server when doing GET and POST requests (okay, there are DELETE and PUSH oddballs, but they aren't used much), often making the client reload.

If you just want it to not *show up* in the URL, you can just use POST, in which case the value of the variable is transmitted in the HTTP request. The only way to submit a POST request without using javascript is with a form and a submit button, AFAIK.

Why don't you want it to show up in the URL? So that people can't link to it? If so, you making things deliberately inconvenient for other people (and this is bad for the same reason frames are bad).

If you're unkeen on having the client reload just from changing a session variable, store it in some javascript or perhaps in an <input type="hidden"> form element, and send the form whenever the user needs a reload (think lazy evaluation here). If the user might go to several different URLs on a reload, have a hidden element saying which one, and have the server send a `302 (I think) redirect' to that url.

Hope this helps.
 
Old 08-07-2006, 01:59 PM   #4
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
I definitely understand what you are both saying. I think I am approaching this from the wrong angle. I did have one other question before I completely abandon this angle. Do you know if it is possible to somehow grab everything that is in the URL, for instance:

http://www.whatever.com/?somevar=3&othervar=jim

Is there anyway I can write something in the script to give me that part of the url : ?somevar=3&othervar=jim

So I could set: $x = function(urlinfo) (?somevar=3&othervar=jim; (where some function gave me this info))

So I could say:

$_SESSION['urlstr'] = $x;

I know i could get individual variables with a GET or POST, but what about the whole string if I didn't know what variables to expect each time. This may be completely impossible, I just thought it might be useful to know how to do if it is possible.

Thanks Again
 
Old 08-07-2006, 02:26 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
If what you want is just the query string, I found this on google.com ("php query string"):

PHP Code:
echo $_SERVER['QUERY_STRING']; 
See also http://ww.php.net/reserved.variables -- you might find something useful.
 
Old 08-07-2006, 03:09 PM   #6
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Yes! That is exactly what I was looking for!!!! Thanks you so much!

This should basically allow me to snapshot the URL at certain points in the operation of the script.
 
  


Reply


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 Coding Question joelhop Programming 12 07-24-2006 06:52 PM
PHP Coding Question joelhop Programming 2 07-20-2006 07:16 PM
PHP 4.4.0 Coding Error Phaux Linux - Software 1 12-30-2005 08:05 PM
coding a php server mrobertson Linux - General 22 06-05-2005 02:06 PM
php coding with knoppix mrobertson Linux - General 1 06-01-2005 02:30 PM

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

All times are GMT -5. The time now is 01:13 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
Open Source Consulting | Domain Registration