Jump to content

Hello :D Setting a session variable via a hyperlink, then redirect


nafetski

Recommended Posts

I usually have my nose in photoshop or flash, so PHP is still very new to me.  The project I've been given is to have a spanish version of our cardholder site.  The way I've achieved it so far is..

 

Create a php file that has all of the english/spanish translations in arrays.  An example would be

 

$LINKS = array(
"Home"=>array("Home","Home_SP"),
"Reloadable_Cards"=>array("Reloadable Cards","Reloadable Cards_SP"),
"Gift Cards"=>array("Gift Cards","Gift Cards_SP"),
);

 

Then I declare the 0 index as english, and the 1 index as spanish.

 

I then use the following function to output the data

 

function getLinks($linkName){

global $ENGLISH, $SPANISH, $LINKS;

// Checking current language //
  if (!isset($_SESSION['CURRENT_LANG'])){
        $lang = $ENGLISH;
   }
   else {
        $lang = $_SESSION['CURRENT_LANG'];
   }
return $LINKS[$linkName][$lang];

}

 

So that way when I call the function getLinks with the array name, it will output the translation based on the session variable.

 

Now, the problem that I'm having an awkward time with is actually declaring the session variable, then getting back to the page I was just at.  I have 2 flags at the top of our page (one is english, one is spanish) and when they click it they are sent to a new php page, that looks like this.

 

<?php

require_once("/dms/php/libs/language/translationdata.php");
GLOBAL $SPANISH;
session_start();
$_SESSION['CURRENT_LANG'] = $SPANISH;

?>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://localhost/mbeu">

 

Pretty simple (tho not sure if this is the correct way to go about things) however, the part of the code that I'm not happy with is the redirect.  Currently it sends you back to the initial index of the site, but if they decide to switch translations then it's a very awkward transition.  (having to renavigate to where they came from)

 

What I would like to do is set the variable, then go right back to the page they were viewing.  I tried using

 

<A HREF="javascript:history.back()">

to accomplish this, but all it ends up doing is getting stuck in a browser loop.  I'm sure there has to be some industry standard way to do this, and any help would be GREATLY appreciated.

 

Thanks for any help!

 

 

Link to comment
Share on other sites

Helllo,

 

Wants to say a word with you. when you are using global variables in a php page, they will be available in the same. They won't be accessible in the next page you post the variables. if you want them in the next page, then you can use the sessions.

 

regarding second problem. when you used href link it is going to starting page. If you want to go back to the previous page, check what are the variables that are required to be in that page(It mught be redirecting the page due to lack of some varibales).

 

if you use href with appending the required variables..you could be solve this issue

 

another option is php include option.

 

Try these options and let me know if you have any issues with it.

 

Best of luck

 

Joshi.

Link to comment
Share on other sites

Thanks for the reply =) 

 

Right now I already have a pretty large number of includes.  The header and footer are both includes (and in the header is the link for the spanish and english php page that sets the session variable)  Since it's an include, and it's used on hundreds of pages on the site - using a relative link isn't really an option for me.

 

I'm not really sure what you mean by missing a variable.  I'm still really new, could you be more specific?

Link to comment
Share on other sites

Shameless self bump, and I think I'm getting closer =P

 

Instead of using a metaredirect I'm using

<?php

require_once("/dms/php/libs/language/translationdata.php");
GLOBAL $SPANISH;
session_start();
$_SESSION['CURRENT_LANG'] = $SPANISH;

Header ("Location: http://localhost/mbeu/index.php");
?>

 

As a way to set the session variable to spanish, then to redirect back to the root of the site.  However, what I would LIKE to do is...

 

In the header, I have added

 

$_SESSION['currentpage'] = $_SERVER['PHP_SELF'];

 

Is there a way that using the header redirect, that I can input the session variable in?  That way it will redirect them to whatever page they were at last! =D

 

Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.