Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define a GET variable and transmit it through URL's

Status
Not open for further replies.

SimoBk

Technical User
Aug 14, 2002
10
CA
[NOTE: I originally posted this on the JS section, thinking it would be a better solution, but been told it would be better to do it server side]

Hi all,

Well, actually my question isn't really "How to do", but "How to do it easier"...

What I want :
- A link on every page of my site that should define a GET variable. By clicking on that link from this page, we'd get the following page :
Code:
      index.php            >>    index.php?var=1
      index.php?a=b        >>    index.php?a=b&var=1
      index.php?var=2&a=b  >>    index.php?var=1&a=b

- The variable should then be transmitted from page to page each time I click a link on any page. So, if I click on a link to "main.php" from this page, I'd get the following page :
Code:
      index.php            >>    main.php
      index.php?a=b        >>    main.php
      index.php?var=2&a=b  >>    main.php?var=2

I know it can be done by :
- Using 10's of lines of code that will get the URL, look for ? and &... , and replace depending on what it find... etc.
- Modify each single A HREF by links generated with PHP.

But I am sure there's a simplier way that I just don't now...

Thanks for any help ;)

Simo

 
Have you thought about using sessions?

The standard way of using sessions in PHP is with a cookie that holds a SSID (session ID). That cookie is transferred with every request of the browser and the server retrieves the values from a session store (a small file in a temp directory, or if enhanced a record in a database table).

ALl you need to implement sessions is to call session_start() at the top of each page that should have the session variables available.
Code:
<?php
session_start();
# some more code ....
#
# set a variable
$_SESSION['var1'] = 'foo';
?>
Now any page that follows and has a session_start() will have $_SESSION['var1'] set to 'foo'.
 
Hi DRJ478,

Yes, actually, the first thing I thought about was a using session variable. But I realized that it would cause problems with search engines...

Let me make it clear, a SE will see:
main.php?lang=en
and
main.php?lang=fr
as two different pages... Which is what I want... If I use session variables, SE will see only a single default version of the page...

So any help? :)

Thanks,

Simo

 
Once you have the session var set it should be easy to just populate the URL with that.

How are the links created? Are they in static HTML or does PHP create all of them?
 
Well, some of the links are static HTML, some are generated by PHP, and some are embed in flash movies :)

That's why I would rather find a way to not edit every single link of the site...

Thanks,

Simo

 
I don't think I quite understand what your after Simo.

Would it be a simple case of $var = $_GET['var']+1; on the recieving end? (seeing as how you do not want to change your already existing links?)

And it seems the second example does something different. Do you have a test we can see?

Sorry if I am missing something obvious.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top