Home » Php » How to capture client browser address bar when click to an icon – PHP

How to capture client browser address bar when click to an icon – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

Hello everyone,

My site needs to support 3 different languages that runs under phpnuke 8.0, phpnuke provides language interface select function that allows switching from different interface, namely using different language files.

Every time when user is inside a module, when they click the language interface selection, it send the user back to the index page. I had found out a typical browser address to switch from language would read : http://mysite.com/ndex.php?newlang=english

I had tried manually to alter the newlang=xxxx and it works and so as inside any other modules that won’t send me back to index page. Therefore I could added different links in the module to let user switching language interface without leaving the module.

Here is what I want to achieve, I want to write a small function when user click a flag or link, the function capture the client’s browser address and append the newlang=xxx and send it to the server to request the proper language. In this case I don’t have to worry about adding links to every module manually.

What I really need is the code or php function to capture the user’s browser address bar

Thanks in advance

Please help

A(Answer):

Heya, osward.

Simply set a session variable, and then use that session variable when fetching content.

For example, create a page called setLang.php (or whatever you’d like to call it):


session_start();
switch( $_GET['lang'] )
{
case 'en_us':
case 'en':
case 'lojban':
.
.
.
$_SESSION['lang'] = $_GET['lang'];
break;
}
header('Location: ' . $_SERVER['HTTP_REFERER']);

Then, put the link on the home page:


<a href="setLang.php?lang=en_us"><img src="picture_of_us_flag.png" /></a>
<a href="setLang.php?lang=en"><img src="picture_of_uk_flag.png" /></a>
<a href="setLang.php?lang=lojban"><img src="picture_of_dr_james_browns_head.png" /></a>

And so on.

Then, when you are fetching content, simply reference $_SESSION[‘lang’] to determine which language to use:


session_start();
if( empty($_SESSION['lang']) )
{
$_SESSION['lang'] = 'en_us';
}
$__lang =& $_SESSION['lang'];

A(Answer):

Thanks for the tips and code example. I think you had pointed me to the right direction. I know phpnuke use cookie and maybe I can make use of it.

However, if I have to make use of the section cookie, I got to know the present url of the user not their language interface they are using. What I want to do is to create an icon with link like this
e.g.

<a href=\"/$url&newlang=english\"><img src=\"images/Eng.gif\" border=\"0\" width=\"12\" height=\"14\" alt=\""._ENG."\" title=\""._ENG."\" hspace=\"3\" vspace=\"3\"></a>

where the $url is

<a href=\"/modules.php?newlang=english&name=event\"><img src=\"images/Eng.gif\" border=\"0\" width=\"12\" height=\"14\" alt=\""._ENG."\" title=\""._ENG."\" hspace=\"3\" vspace=\"3\"></a>

You might want to have a look at my test site at http://cf-home.cn, There are 3 flags in the middle of the header section, which is doing exactly what you suggested me to do. (Note the address bar of your browser) Please click the module feedback and you will find 3 language interface icons there that I manually added the link http://www.cf-home.cn/modules.php?ne…&name=Feedback for english

What I really want to do is when user click the Zh-tw icon for example, he sends the present url plus append &newlang=zh-tw to the server

Any idea of how to achieve this?

A(Answer):

Heya, osward.

You can get the relative URI of the current page by using $_SERVER[‘SCRIPT_NAME’].

A(Answer):

Heya, osward.

You can get the relative URI of the current page by using $_SERVER[‘SCRIPT_NAME’].

The $_SERVER[‘SCRIPT_NAME’] only returns http://www.cf-home.cn/modules.php and the address bar reads http://www.cf-home.cn/modules.php?name=Feedback.

Anything I am missing from your reply?

A(Answer):

Heya, osward.

If you want to include the variables, use $_SERVER[‘PHP_SELF’];

Note that to prevent XSS when outputting $_SERVER[‘PHP_SELF’] in an HTML document, you’ll want to run it through htmlspecialchars() first.

A(Answer):

I found $url = $_SERVER[‘FULL_URL’]; works best for me, the interesting thing is hat when I made the first a switch eg, from English -> Zh-cn-utf8 (Simplify Chinese), it works fine and the address bar is exactly as I did it manually, however if I click English again, the &newlang=English just append to the last url but still it works

I would like to convert the first capture url as string and use it in the link, how am I converting the first capture url (ie when user first visit the page) into a string? Right now the $_SERVER(‘FULL_URL’] keep returing url everytime the link is hit.

A(Answer):

Heya, osward.

You’ll probably want to build it manually.

If you have this in the head of your HTML:


<base href="http://<?php echo $_SERVER['HTTP_HOST']; ?>" />

Then you can make your link prefix as follows:


$_linkPrefix = $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($_GET);

You may also find add_rewrite_var() to be useful.

A(Answer):

Heya, osward.

You’ll probably want to build it manually.

If you have this in the head of your HTML:


<base href="http://<?php echo $_SERVER['HTTP_HOST']; ?>" />

Then you can make your link prefix as follows:


$_linkPrefix = $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($_GET);

You may also find add_rewrite_var() to be useful.

Hi pbmods,

Because I am not quite sure of what you menat in the reply

I found a script as follows:
[PHP]$_SERVER[‘FULL_URL’] = ‘http’;
$script_name = ”;
if(isset($_SERVER[‘REQUEST_URI’])) {
$script_name = $_SERVER[‘REQUEST_URI’];
} else {
$script_name = $_SERVER[‘PHP_SELF’];
if($_SERVER[‘QUERY_STRING’]>’ ‘) {
$script_name .= ‘?’.$_SERVER[‘QUERY_STRING’];
}
}
[/PHP]

I am using the $script_name and append the language selection &newlang=english as the link. I put the link in the index page and I could change language interface anywhere except the index page. It returns a 404 error and the address bar reads http://mydomain.index.php&newlang-english.
In the above } else {
I am sure it meant the same as you instructed me to do, add a ‘?’ to the $_SERVER[‘PHP_SELF’] but still couldn’t add the ‘?’ at the index page

Am I missing anywhere on my part? I also try the str_replace like
[PHP]} else {
$script_name = $_SERVER[‘PHP_SELF’];
if($_SERVER[‘QUERY_STRING’]>’ ‘) {
$script_name .= str_replace("php", "php?", $_SERVER[‘PHP_SELF’]).$_SERVER[‘QUERY_STRING’];
}[/PHP] and yet could not at the ‘?’ at the index page. Please help

Thanks in advance

A(Answer):

Heya, osward.

It’s never getting to the else in your code because $_SERVER[‘REQUEST_URI’] exists.

If you’re trying to replace the value in each URL, consider modifying $_GET and using http_build_query() as I suggested above. Since each array key can only have one corresponding value, this will ensure that each _GET variable is only output once in the query string, even if its value has changed.

A(Answer):

Heya, osward.

It’s never getting to the else in your code because $_SERVER[‘REQUEST_URI’] exists.

If you’re trying to replace the value in each URL, consider modifying $_GET and using http_build_query() as I suggested above. Since each array key can only have one corresponding value, this will ensure that each _GET variable is only output once in the query string, even if its value has changed.

Hi, pbmod,

I finally got it done by appending &newlang=english to your pervious code posted

$_linkPrefix = $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($_GET);

The link to the English interface now read $_linkPrefix&newlang=english

However, I still don’t understand and haven’t use the first part of the code you posted

<base href="http://<?php echo $_SERVER['HTTP_HOST']; ?>" />

What do you mean by

If you have this in the head of your HTML:

I don’t have any HTML documents here through the phpnuke. Am I missing you by a beat?

A(Answer):

Heya, Osward.

The base tag allows you to specify sort of the ‘prefix’ for every link on your page.

I’m not too familiar with phpnuke specifically, but like any CMS, there should be a header file that you can edit that contains the HTML that goes at the top of every page.

Adding a base tag is not strictly necessary, but it makes it a lot easier to, for example, migrate your site to a new server or to make it easier to keep your link behavior consistent.

For more information, check out this article.