Page 1 of 1

Passing variables through a link.

Posted: Wed Dec 28, 2005 6:52 pm
by bsands
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

Re: Passing variables through a link.

Posted: Wed Dec 28, 2005 7:27 pm
by Chris Corbyn
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
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 us :(

Passing variables through a link.

Posted: Wed Dec 28, 2005 11:53 pm
by khaki_monster
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.

Posted: Thu Dec 29, 2005 12:13 am
by n00b Saibot
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.

Code: Select all

$name = $_POST['name'];
$pass = $_POST['pass'];

$result = mysql_query("SELECT COUNT(*) FROM tblUsers WHERE name='{$name}' AND pass='{$pass}'");
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 :wink:

Posted: Thu Dec 29, 2005 12:22 am
by AKA Panama Jack
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.

Code: Select all

if (!isset($_SESSION['user_id']))
{
	session_start();
}

$_SESSION['user_id'] = $user_info['user_id'];
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.

Code: Select all

echo $_SESSION['user_id'];
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.

Posted: Thu Dec 29, 2005 2:14 am
by Jenk
AKA Panama Jack wrote:

Code: Select all

if (!isset($_SESSION['user_id']))
{
	session_start();
}

$_SESSION['user_id'] = $user_info['user_id'];
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'].

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.

Posted: Thu Dec 29, 2005 4:52 am
by AKA Panama Jack
Jenk wrote:
AKA Panama Jack wrote:

Code: Select all

if (!isset($_SESSION['user_id']))
{
	session_start();
}

$_SESSION['user_id'] = $user_info['user_id'];
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'].
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.

Code: Select all

if (session_id() == "")
{
	session_start();
}

$_SESSION['user_id'] = $user_info['user_id'];
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.
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.

You could have the final process of the login perform

Code: Select all

session_start();
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
//etc and whatever
You would perform the above when a successful login has been achived before anything else has been sent to the browser and never see any header errors. There are many, many ways this can be accomplished and I have just given two simple examples. :)

Posted: Thu Dec 29, 2005 6:37 am
by Jenk
your example is sloppy coding, hence my point.

Code: Select all

<?php
session_start();

//code..

if (!isset($_SESSION['var'])) {
    session_start(); //header errors - headers already sent on line 1.
}

?>
If you really must check for the session before calling session_start(), use:

Code: Select all

if (session_id() == '') {
    session_start();
}
but then.. you know you will be wanting to access the session variables - simply by using $_SESSION dictates this to you..

Re: Passing variables through a link.

Posted: Thu Dec 29, 2005 6:57 am
by BDKR
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
Since it seems nobody has answered your question, I'll give it a go.

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. :wink:

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>
What you're really after in the above example is the name of the celeb. When your script is fired up, it's going to look at this form and create two variables. One called "action" and the other being "celeb" and they'll be part of your $_POST array of values. Just do ...

Code: Select all

print_r($_POST);
... to see them.

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>
On the script side, the same thing applies except that to get the vars from this you need to venture into your $_GET array of values.

That should get you started. There are also HTML references about on the net. Google is your freind.