单击链接设置会话变量

how do i set session variables on click of a link. I have many links on one page. based on the link clicked a session variable has to be set. For instance: i have 5 links

<a
 href="page1.php">
page1
</a>

<a href="page2.php">
page2</a>

<a href="page3.php">
page3</a>


<a href="page4.php">
page4</a>

<a href="page5.php">
page5</a>

if the first link is clicked then $_SESSION['category']=page1 is to be set

if the second link is clicked then $_SESSION['category']=page2 is to be set and so on.. how do i get this?? any idea??

well you can always mix javascript with php.use the onclick event for the anchor tag. when the link is clicked just set your session in the javascript written for the onclick event.hope this make sense

Your best bet is to set the $_SESSION['category'] at the beginning of each of those php files. If that is not an option, perhaps create a 'page.php' file that accepts a query string.

Something like this:

<?php //example: page.php?p=1
// don't forget to clean $_GET['p']
$p = $_GET['p'];
$_SESSION['category'] = $p;
include("page$p.php");

On each page you will need to set the session.

For example on page1.php :

<?php
  session_start();
  $_SESSION['category']='page1';
?>
Any content on page1.php

and so on for each page.

If however you are wanting it to sit on the same page, but set the session and use that session to control something on the page (e.g. the category), then you could pass a parameter through the URL and set the session that way, e.g. an example would be page.php?category=page1

<?php
  session_start();
  if (isset($_GET['category'])) {
    $_SESSION['category']=$_GET['category'];
  }
?>
Any content on page.php

You don't really need sessions to detect the current page. Instead you can use this:

<?php
    $pageName = basename($_SERVER[PHP_SELF]);
?>

I suppose you could then save that to a session variable, but it's going to be overwritten on every page.

As gavinbear's suggestion for putting at the top of the page, you could use the following to get a match to your original naming convention:

$requested_uri = $_SERVER['REQUEST_URI']
$requested_path = parse_url($requested_uri)[path]
$_SESSION['category'] = basename($requested_path, ".php")