LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php:passing a var by a link by using session (https://www.linuxquestions.org/questions/programming-9/php-passing-a-var-by-a-link-by-using-session-26899/)

norbs 07-31-2002 08:59 AM

php:passing a var by a link by using session
 
in php

I want to make a link to another page
so
<A HREF="/smthg.php">blabla</A>

The problem is that i want to pass some variables to the smthg.php page

I have to open a session by session_start, and then using session_register : no problem w/ that, it works perfectly...

thanks for the master who can answer me.

lackluster 07-31-2002 09:51 AM

I'm no master but depending on your environment, you need to pass sid from all your links :
Code:

<a href="this.php<?=sid?>">this</a>
Also, session_start() continues a session so call it on subsequent pages.

norbs 08-01-2002 03:10 AM

ok for the SID.

for exemple:

<?php
session_start();
session_register("var");
?>

<A HREF="smthg.php?<? print SID; ?>">blabla</A>

I want for exemple that a variable $var take the value of 5 when clicking the link blabla

lackluster 08-01-2002 01:41 PM

okay...... what's the problem then?

at the top of smthg.php put the following :
<?
session_start()
if (isset($var)) echo("Var is set to $var");
else $var = 5;?>

norbs 08-02-2002 01:57 PM

But I want that $var have different value depending the link
I've read that I have to do smthg like :

<A HREF="smthg.php?<? print $var=5; ?>">link1</A>
<A HREF="smthg.php?<? print $var=10; ?>">link2</A>

The problem is that when i'm using the link1, i've got $var=10 ?!!

lackluster 08-02-2002 03:50 PM

Okay, I get it now. Here's the deal ....

ASP, CFusion, PHP, CGI (Perl or whatever) all of this web language stuff have common properties. One of which whose concept you need to grasp is this : These languages run on the server and return HTML. Once the HTML is sent back, you cannot modify the script's variables. If you like we can get more in depth about this and why you can't do what you want to do, but hopefully this will suffice.

Here is an alternitive :
Use flags to tell the next page you want to set this variable. Like this : <a href = "page2.php?set_var=5">link</a> <a href = "page3.php?set_var=10">link2</a> and then on the other pages just do something like this :
<? if(isset($set_var)) {
$var = $set_var;
}?>

Cool? :)


All times are GMT -5. The time now is 06:48 AM.