开发者

Language choice button URL adapted from current URL

开发者 https://www.devze.com 2023-03-16 04:14 出处:网络
Our website has two language versions, Czech and English. We have a simple flag in the top right of each page (within header.php) that directs to either index_cz.php for the Czech index and index.php

Our website has two language versions, Czech and English. We have a simple flag in the top right of each page (within header.php) that directs to either index_cz.php for the Czech index and index.php for the English index for all pages regardless of the current URL.

<a href="index_cz.php" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="index.php" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version">

I would like something more sophisticated using PHP that changes the URL of the buttons depending on what the current page URL is. For example: if the current page is 'new.php' I would like the Czech button to now point to 'new_cz.php' and the English button to be 'new.php'.

The pages that include ?id= endings (with whatever id is as开发者_StackOverflow社区sociated with that page) use a separate header.php and reference the appropriate id from the database for example:

<a href="project_cz.php?id=<?php echo $det[0];?>" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>    
<a href="project.php?id=<?php echo $det[0];?>" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version"></a>

Can you think of the appropriate code to handle the standard php pages? Many thanks!


You can access the current file with $_SERVER['PHP_SELF'], but make sure to run htmlspecialchars on it before echoing it to prevent XSS attacks.

That said, you can then str_replace the current file to get the czech version:

str_replace('.', '_cz.', $_SERVER['PHP_SELF'])

the other way is easy, too:

str_replace('_cz.', '.', $_SERVER['PHP_SELF'])
0

精彩评论

暂无评论...
验证码 换一张
取 消