Hi All,

I'm new to php and building my first site that will require session management. I've got a "splash page" where users will be required to select a region which I want to record into $_Session['location'].

I'd like to have the various region options be links:

<a href="/ny">New York</a>
<a href="/ma">Massachusetts</a>

etc.

When a user clicks one of these links, I want this to somehow trigger the assignment of $_Session['location']. Then i want to take users to "page1.php".

From what I've read, it is not kosher to mix client-side Javascript (e.g. onClick) with server-side php. So -- is there a solution that's reasonably elegant? Or is this simply going against the grain of how php is meant to be used?

A possible solution I've thought about is passing the location to an intermediate page as a parameter in the URL, as in <a href="/location_redirect.php?location=ny">. Then in location_redirect.php, set the session variable with $SESSION['location'] = $GET['location']. Then redirect to "page1.php" -- though I don't know the details of how to do this redirect.

Thanks for any pointers. I'd be grateful for specific ideas or general thoughts about whether this is a reasonable goal, or if there are better ways to capture a session variable from a user action.

    why not simply link directly to page1.php with the state appended as a GET var and start the session in page1.php?

      Thanks devinemke. If I understand correctly, you mean on the splash page link to

      <a href="/page1.php?location=ny">New York</a>

      I know this would work, but I'd like to avoid having any parameters visible to users in the URL.

        in your original code example...

        jhodes81 wrote:

        <a href="/ny">New York</a>
        <a href="/ma">Massachusetts</a>

        ...you have parameters visible to users in the URL. what's the difference between that and passing GET vars?

          That's true, but I don't want those URLS to be visible to end users. In the original example I was thinking I would somehow "process" those URLS (either go to that page, set the session variable, then redirect to page1.php, or use mod_rewrite, or something).

          My first instinct was to use a JavaScript "onClick" event for each link, but I know I can't mix server-side and client-side code like that.

          But that's the gist of it -- I'd like the links to trigger some sort of action that assigns the session variable and then takes me to page1.php.

          This seems like it should be fairly simple, am I barking up the wrong tree?

            you could have the users pick there region via a form (dropdown or radio group). i don't see how this improves usuability/security in any way but at least the links will not be visible in the address bar of the browser.

              Write a Reply...