Forum Moderators: coopster

Message Too Old, No Replies

Setting SESSION variables with a hyperlink

How to set a session variable and then jump to or reload another page

         

madivad

11:18 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



I have been searching for almost 24hours and can't seem to locate ANY information on something that I feel should be pretty simple. Everything refers to GET and POST, but all I want to do is set a session variable from a hyperlink.

eg, I have a page that has several columns, I have existing code that generates numbers from 3 to 9 and when they are clicked I want a session variable set to that value.

Then retrieve that value when I go to the new page with the reload hyperlink.

I can't use a form or button on this page - for asthetic reasons it has to be a hyperlink.

Ideally, I would like to press the hyperlink and it set's the value of the session variable and then reloads the page. but this seems to be impossible with PHP.

What am I doing wrong?

Here is my code:


<p>Columns:
<?php
for($i=3; $i<9; $i++)
{
echo "<a href='javascript:SetCols($i);'>$i</a>\n";
}?>
<font size="-2"><a href="links_main.php">RELOAD</a></p>

I know that the javascript must be wrong (as the section is never called) so what can I do to fix it?

webadept

11:45 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Well, you could be makeing it too hard. What's wrong with a link that looks like this?

<a href="mysession_tracker.php?session=3&user=me">3</a>

this have that page call this one back for the refresh.

Am I missing something?

madivad

12:13 am on Feb 21, 2004 (gmt 0)

10+ Year Member



Well, I wanted to handle it all in page, but I have taken t on board and run a test page. It's doing what I want it to do, so now I'll go and implement it into my real page.

Sometimes it's hard to see the forest for the trees.

In short, my test pages use simple counters and page1 and page2 are the same except for obvious differences:


<?php
if (!session_id()) session_start();
if (isset($_SESSION['Count']))
{
$_SESSION['Count']++;
}
else
{
$_SESSION['Count']=1;
}
if (isset($_SESSION['Page1']))
{
$_SESSION['Page1']++;
}
else
{
$_SESSION['Page1']=1;
}
?>
<html>
<head>
<title>Test Page Sessions</title>
</head>
<body>
<?php echo "Count: ".$_SESSION['Count']."<br>\n";?>
<?php echo "Page1: ".$_SESSION['Page1']."<br>\n";?>
<?php echo "Page2: ".$_SESSION['Page2']."<br>\n";?>
<a href="page1.php">page1</a> :
<a href="page2.php">page2</a> :
<a href="pager.php?r=1">RESET1</a> :
<a href="pager.php?r=2">RESET2</a>
</html>

and this is the interjoining page:

<?php
if (!session_id()) session_start();
if (isset($_REQUEST['r']))
{
if(1==$_REQUEST['r'])
{
$_SESSION['Page1']=0;
}
elseif(2==$_REQUEST['r'])
{
$_SESSION['Page2']=0;
}
}
$_SESSION['Page1']--;
header("Location: page1.php");
?>

This works, but is there a better way? And what other sort of validatio should I do? eg, dreamweaver always inserts some action for testing for "MAGIC_QUOTES" do I need to do this sort of thing?

Thanks for your speedy reply. It really helped!

webadept

4:12 am on Feb 21, 2004 (gmt 0)

10+ Year Member



Really there is no reason it can't call it self, but since you are using the inline hyper GET set there, this way keeps the URL address clean, and later, when you want to add in some error checking (for instance, I mess with the string and put in a 21, just to see what would happen using the URL bar), you can do that will very little fuss, having your control script send the user with a bad address to a warning page, or off your site, or what ever.

It is good practice to keep code and HTML separate as much as possible.

Changing the SESSION_ID value is kind of working against yourself as a security thing. The whole idea around SESSIONS as I understand them, is having a tracker which is unpredictable. Setting that to a predictable number, you might as well use a cookie reference, unless you have some other reason for using the SESSIONS, (for instance you are just experimenting and what to see how much you can mess with things, which is a time honored method of learning any new language).

The "best way" is a difficult question, for me, because I don't really know your final need. What it appears you are doing is trying to find a comfortable way of "Maintaining State".

Here are some webpages which might give you some ideas, and help you understand what you are working with.

[phpbuilder.com...]

[wdvl.com...]

[zend.com...]

Hope that helps,