Passing variables through a link.
Moderator: General Moderators
Passing variables through a link.
Question:
If I want people to be able to pull up everything pertaining to the link they click, how can I go about setting this up? What I mean by this is:
Someone clicks on the link Recent Searches
This opens a second page and passes the variables we used on the first page, such as Username, Link Name, and so forth. The second page would use these variables to look through the database.
Any idea or is my writing too cryptic?
Any help would be appreciated.
-Brian
If I want people to be able to pull up everything pertaining to the link they click, how can I go about setting this up? What I mean by this is:
Someone clicks on the link Recent Searches
This opens a second page and passes the variables we used on the first page, such as Username, Link Name, and so forth. The second page would use these variables to look through the database.
Any idea or is my writing too cryptic?
Any help would be appreciated.
-Brian
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Passing variables through a link.
Your question isn't cryptic... it's just too general. You simply need to build a MySQL (or Postgres) query on the page based upon those variables. That's as much info as I can give you based upon the info you gave usbsands wrote:Question:
If I want people to be able to pull up everything pertaining to the link they click, how can I go about setting this up? What I mean by this is:
Someone clicks on the link Recent Searches
This opens a second page and passes the variables we used on the first page, such as Username, Link Name, and so forth. The second page would use these variables to look through the database.
Any idea or is my writing too cryptic?
Any help would be appreciated.
-Brian
- khaki_monster
- Forum Commoner
- Posts: 73
- Joined: Tue Oct 11, 2005 12:36 am
- Location: Philippines
- Contact:
Passing variables through a link.
hello pepz,
i just like to make a followup question to d11wtq. if you could please give us of some example(s) of what your saying.
seemz like your the right person to ask to enlighten our minds
cheerz.
i just like to make a followup question to d11wtq. if you could please give us of some example(s) of what your saying.
seemz like your the right person to ask to enlighten our minds
cheerz.
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
I presume you have exp. passing arround vars thru GET/POST,
I presume you have exp. querying MySQL DB,
putting both together should do it for you...
one eg.
typical login eg. here you get the name/pass from user and use it to check wherther the user exists or not. In the same way you can use the vars for other purposes like displaying a user's details or last month's sales records 
I presume you have exp. querying MySQL DB,
putting both together should do it for you...
one eg.
Code: Select all
$name = $_POST['name'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT COUNT(*) FROM tblUsers WHERE name='{$name}' AND pass='{$pass}'");- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Your best bet is to use sessions. This way the person gets their own unique session id and you can add data to the $_SESSION array that will automatically follow the client from page to page on the site. You do not even need to update the $_SESSION data after you assign it.
In the above example if there isn't a user_id in the session array then start a new session. Then it populates the $_SESSION['user_id'] variable with the you provide. You can do the same thing with email addresses, passwords, ect.
The data will be automatically passed between pages and you won't need to do anything more to make that happen.
You can do something like the above to use the passed session data. This way you can keep data private and it isn't passed through get or post variables.
Using a database to do this adds alot of unneeded overhead and makes things far more complex than they need to be.
Code: Select all
if (!isset($_SESSION['user_id']))
{
session_start();
}
$_SESSION['user_id'] = $user_info['user_id'];The data will be automatically passed between pages and you won't need to do anything more to make that happen.
Code: Select all
echo $_SESSION['user_id'];Using a database to do this adds alot of unneeded overhead and makes things far more complex than they need to be.
That's not the best logic for using session var's, simply because you want to start the session regardless of the existence of $_SESSION['user_id'].AKA Panama Jack wrote:Code: Select all
if (!isset($_SESSION['user_id'])) { session_start(); } $_SESSION['user_id'] = $user_info['user_id'];
You know you will want access to $_SESSION, so you know you need to start the session..
What if $_SESSION['user_id'] doesn't exist - but session_start() has already been run? You'll get a header error.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Not really... You shouldn't start a session unless you really need a session. It is really a waste of resources to start a session for a guest on most sites. I know that many people start a session as soon as any browser accesses their site but I feel that is just being sloppy and careless. I could have checked to see if a session_id had been assigned before starting the session instead of checking for a user_id element being set.Jenk wrote:That's not the best logic for using session var's, simply because you want to start the session regardless of the existence of $_SESSION['user_id'].AKA Panama Jack wrote:Code: Select all
if (!isset($_SESSION['user_id'])) { session_start(); } $_SESSION['user_id'] = $user_info['user_id'];
Code: Select all
if (session_id() == "")
{
session_start();
}
$_SESSION['user_id'] = $user_info['user_id'];That is why you only have the one session_start(). There wouldn't be any real reason, except for sloppy coding, to create a session before it is needed. You could use anything as a detection parameter but in my example I used user_id. It could be anything that the programmer wants to use as an indicator that the person has logged in or that a session is needed.Jenk wrote:You know you will want access to $_SESSION, so you know you need to start the session..
What if $_SESSION['user_id'] doesn't exist - but session_start() has already been run? You'll get a header error.
You could have the final process of the login perform
Code: Select all
session_start();
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
//etc and whateveryour example is sloppy coding, hence my point.
If you really must check for the session before calling session_start(), use:
but then.. you know you will be wanting to access the session variables - simply by using $_SESSION dictates this to you..
Code: Select all
<?php
session_start();
//code..
if (!isset($_SESSION['var'])) {
session_start(); //header errors - headers already sent on line 1.
}
?>Code: Select all
if (session_id() == '') {
session_start();
}Re: Passing variables through a link.
Since it seems nobody has answered your question, I'll give it a go.bsands wrote:Question:
If I want people to be able to pull up everything pertaining to the link they click, how can I go about setting this up? What I mean by this is:
Someone clicks on the link Recent Searches
This opens a second page and passes the variables we used on the first page, such as Username, Link Name, and so forth. The second page would use these variables to look through the database.
Any idea or is my writing too cryptic?
Any help would be appreciated.
-Brian
Yes, passing variables from page to page is no big deal and is done all the time. The first stage is to create something that will pass the variable in the first place. An HTML form is a good example. Just study the particulars of that in an HTML reference. Easy as cake for the most part but here is a short example.
Code: Select all
<html>
<form name="test_form" method="post" action="target_url">
Input fav celeb: <input type="text" name="celeb">
<input type="submit" name="action" value="Submit">
</form>
</html>Code: Select all
print_r($_POST);Now if you want to put them right in the URL, then you need to create a line something like ...
Code: Select all
<a href="celeb_search.php?celeb=Amber_Tamblyn&action=get_info">Amber Tamblyn</a>That should get you started. There are also HTML references about on the net. Google is your freind.