3

I have a links such as:

<a href="index.php?lang=en"><img src="images/uk.png" style="height:20px"/></a>

And and included page in index.php:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'gr':
  $lang_file = 'lang.gr.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>

It changes the URL to index.php?lang=gr reads the parameter LANG and translates the page depends on this parameter.

How can I change my code to do it without passing the parameter to Url? What I mean is, that I want a user to stay on the same page and change the language on page refresh.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Andrey Popov
  • 163
  • 4
  • 12

2 Answers2

1

It would be as below ;

        <input type="submit" value="" class=<?php if( $_SESSION['lang']  == "tr" ) { echo "submittr";  } else { echo "submiten";} ?>>

In that case you will be changed type of the class

Egemen
  • 61
  • 1
  • 1
  • 8
0

There are a few ways to do this, one of which is to use a form and make the "submit" button your image. The form will use a POST method which will not include the variables in the URL. A get request will add ?lang=en

<div id="buttons">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
        <input type="hidden" name="lang" value="en">
        <button type="submit">
            <img src="images/uk.png" style="height:20px"/>
        </button>
    </form>
</div>

Change your code to use the $_POST variable:

if(isSet($_POST['lang']))
{
$lang = $_POST['lang'];
immulatin
  • 2,118
  • 1
  • 12
  • 13
  • @AndreyPopov Try changing the above code from `
    – Funk Forty Niner Jun 12 '13 at 17:02
  • Yes, I think it works now, Thank you! But...it still puts a parametere to url (it is what i get on mouse click): http://localhost:8888/index.php?lang=gr – Andrey Popov Jun 12 '13 at 17:08
  • @AndreyPopov You're welcome. But, what do you mean by *"it still puts a parameter to url"*? I don't quite understand what you mean by that. Can you elaborate? `GET` will always show a parameter. You will have to to a `mod_rewrite` I think to not show one. Something similar to what Facebook does, am I right? – Funk Forty Niner Jun 12 '13 at 17:10
  • sorry you need to update your code to use the $_POST -- I've updated the answer – immulatin Jun 12 '13 at 17:10
  • Yes, this works until I have another parameter in URL... For example: I'm on page: http://localhost:8888/index.php?cat=2 clicking the button will redirect me to http://localhost:8888/index.php Please tell me how to avoid it? Thank you!!! – Andrey Popov Jun 12 '13 at 17:34
  • Is there a reason you are passing all the variables via the URL/GET? You might want to store variables that you need in a SESSION or in a COOKIE – immulatin Jun 12 '13 at 17:43
  • @AndreyPopov Here's one link you can read up on. And you can just **Google** `how to mod_rewrite` => http://www.codingforums.com/showthread.php?t=215977 and http://wettone.com/code/clean-urls - Also consult on SO `how to enable mod_rewrite` if not enabled: http://stackoverflow.com/questions/9021425/how-to-check-if-mod-rewrite-is-enabled-in-php – Funk Forty Niner Jun 12 '13 at 17:44
  • @AndreyPopov Here's another pretty good article on the subject: http://www.wisdombay.com/articles/basic%20guide%20to%20mod_rewrite.htm – Funk Forty Niner Jun 12 '13 at 17:49
  • Its ok for me to show all the other variables in Url as it gives an ability for the user to get a direct link to category, object and so on...but language is stored in Cookie, so I dont want to show it in Url... The only question is now how to redirect the user back to the page he was viewing: localhost:8888/index.php?cat=2 on button click as now he is directed to localhost:8888/index.php... Maybe I can change $_SERVER['PHP_SELF'] to something else? – Andrey Popov Jun 12 '13 at 20:19
  • Yes, and it causes the error every time i try to refresh page...it askes if I want to submit the form( – Andrey Popov Jun 12 '13 at 20:41
  • You need to use ajax/jquery/javascript, something client side if you do not want the GET parameters in the URL or you dont want the submit when refreshing the page. GET and POST are the two options for PHP and neither of which will work for you – immulatin Jun 25 '13 at 20:56