Change links on all pages according to PHP - php

Ok my website incorporates different languages, I did:
$lang = isset($_GET['lang']) ? $_GET['lang'] : "";
if (!empty($lang)) {
$curr_lang = $_SESSION['curr_lang'] = $lang;
} else if (isset($_SESSION['curr_lang'])) {
$curr_lang = $_SESSION['curr_lang'];
} else {
$curr_lang = "he";
}
if (!empty($pb20_14users->language == he)) {
echo "&lang=he";
}
if (file_exists("languages/" . $curr_lang . ".php")) {
include "languages/" . $curr_lang . ".php";
} else {
include "languages/he.php";
}
// Returns language key
function lang_key($key)
{
global $arrLang;
$output = "";
if (isset($arrLang[$key])) {
$output = $arrLang[$key];
} else {
$output = str_replace("_", " ", $key);
}
return $output;
}
The text has changed and everything is perfect, but I want to change my URL
&lang=he
But don't go through all the existing pages manually, they were built like this:
<div class="links">
Home<br />
Register<br />
Statistics<br />
Forgot password?
</div>
How do I make all addresses end & lang = he without going through one by one, if it matters I made a table in SQL called language and there is the word he, maybe it will help the thinkers, thank you very much

I recommend you go a (basic) nice url system, your urls will become /en/?page=foo which isnt the biggest rewrite in the url.
RewriteRule ^([a-z]{2})/(.*) /$2&lang=$1 [L]
I havent tested that line, but it should be close. We take everything before the slash (en) and add that to the rest (?page=foo) which results in ?page=foo&lang=en for your script.
Now you can do this and use CURRENT_LANG everywhere in your PHP for the language.
$lang = in_array($_GET['lang'], ['en', 'nl']) ? $_GET['lang'] : 'en';
define('CURRENT_LANG', lang);
Or try to use the <base /> tag in html.

Related

php dealing with apache rewrite urls in the code

in my application i'm currently using "pretty urls" with apache rewrite. So:
article.php?id=123&title=hello-bob
becomes:
/articles/hello-bob.123
In my local dev environment, I'm not using pretty urls and I ended up writing a function to pass links through, to get the correct one based on a setting:
public function get_link($id, $title, $additional = NULL)
{
$link = '';
$nice_title = core::nice_title($title); // remove spaces, special characters and so on so-it-looks-like-this
if ($this->config('pretty_urls') == 1)
{
$link = 'articles/'.$nice_title.'.'.$id;
if ($additional != NULL)
{
$link = $link . '/' . $additional;
}
}
else
{
$link = 'index.php?module=articles_full&aid='.$id.'&title='.$nice_title;
if ($additional != NULL)
{
$link = $link . '&' . $additional;
}
}
return $this->config('website_url') . $link;
}
Is this really wasteful? I'm wondering how other applications and sites manage this, when they allow people to enable pretty urls and show the correct links in the rendered content?

Determine Logo link based on URL using PHP

Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>

A better way to use mod_rewrite & php in translating pages?

I am trying to translate these pages from English to French:
/about.php
/contact.php
/paintings.php?id={$id}&title={$title}
I created the following link:
<?php if($language !== 1): ?>English<?php else: ?>French<?php endif; ?>
With this code behind it:
$url = $_SERVER["REQUEST_URI"];
if (isset($_GET['lang'])) {
$pattern1 = '&lang=' . $_GET['lang'];
$pattern2 = '?lang=' . $_GET['lang'];
$patternTotal = array($pattern1, $pattern2);
$url = str_replace($patternTotal, '', $url);
$language = 1;
}
else if (strpos($_SERVER["REQUEST_URI"], '?')) {
$url .= '&lang=en';
$language = 0;
}
else {
$url .= '?lang=en';
$language = 0;
}
What I want for it to do is to return 1 to $language if the lang parameter is set to any value. Then I will use an if statement to fetch and display the content accordingly.
However I realized that this url /paintings.php?id={$id}&title={$title} looks bad and I used mod_rewrite to get it to this version /{$id}/{$title} with this RewriteRule ^([^/]*)/([^/]*)$ /photo.php?id=$1&title=$2 [L]. And things broke. While the $url works on all the other pages on the paintings one it doesn't.
I have a feeling that I got it all wrong. How can I make it work? Also, is there a more efficient way in doing this?

How can I use a GET url to identify if the user is accessing the backend?

I made a PHP framework with my own templating system, which works as I want it to in the frontend. The templating system uses the ?url=($page) part of the of the URL to figure out what page the user requests, then displays the content of the file with the name ($page). I then use htaccess to "pretty up" the URL.
However, I'm now expanding the framework to identify if $_GET['url'] contains backend, and if it does, see if it has a trailing slash with another string. For example, if the value of $_GET['url'] is backend/manage-gallery, I want it to return the page manage-gallery from within the the backend folder. Here is the code that I have now. Currently, I got it to retrieve a page statically (index.php) if the value of $_GET['url'] is backend.
public function addTpl() {
global $zip;
if(!isset($_GET['url']) || empty($_GET['url'])) {
$_GET['url'] = 'index';
}
$front = '_zip/_templates/_front/'. $zip['Template']['Front'];
$back = '_zip/_templates/_back/'. $zip['Template']['Back'];
if(strpos($_GET['url'], 'backend') !== false) {
if(file_exists($back . '/')) {
if(file_exists($back . '/index.php')) {
ob_start();
include($back . '/index.php');
$this->tpl .= ob_get_contents();
ob_end_clean();
} else {
if($zip['Template']['Front'] == 'Refresh/multi-page') {
ob_start();
include($back . '/404.php');
$this->tpl .= ob_get_contents();
ob_end_clean();
} else {
die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev#gmail.com">here<a/>.</b>'));
}
}
} else {
die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
}
} else {
if(file_exists($front. '/')) {
if(file_exists($front. '/' . secure($_GET['url']) . '.php')) {
ob_start();
include($front. '/' . secure($_GET['url']) . '.php');
$this->tpl .= ob_get_contents();
ob_end_clean();
} else {
if($zip['Template']['Front'] == 'Refresh/multi-page') {
ob_start();
include($front. '/404.php');
$this->tpl .= ob_get_contents();
ob_end_clean();
} else {
die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev#gmail.com">here<a/>.</b>'));
}
}
} else {
die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
}
}
}
How can I make my code check if the get value contains backend, AND see if it has a traling slash with a string after it? Here is my htaccess
RewriteEngine On
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1
Since the slash will always be right after the backend string, I would change the strpos by adding the slash:
if(strpos($_GET['url'], 'backend/') !== false) {
Also, if backend occurs in the string, it will always be at the beginning of the string, so I would look for it at position zero:
if(strpos($_GET['url'], 'backend/') == 0) {
Then, I would use && and add another condition to that if statement to check to see if the string is longer than 7 characters. In other words, if backend/ is at the beginning, and the string is longer that 7 characters, there are more characters after backend/.
if(strpos($_GET['url'], 'backend/') == 0 && strlen($_GET['url']) > 7) {
EDIT: There is a problem in your .htaccess file. Change $1 to $2.

Detect and display current page as marked link

until today I've been using following function
function menu_active($pagename)
{
$active = basename($_SERVER['PHP_SELF'], ".php");
if ($active === $pagename) echo "id='active' ";
}
And in HTML
<?php menu_active('page_name'); ?>
to detect which page is active to mark it as current page, but when I changed my page display method it doesn't work anymore. So I wonder how to change the function to make it work. I've tried to declare $pagename as filename.inc.php in the script below, but no use.
I'm using this script to display pages
$pages_dir = 'pages';
if (!empty($_GET['p']))
{
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p . '.inc.php', $pages))
{
include ($pages_dir . '/' . $p . '.inc.php');
}
else
{
echo 'Error';
}
}
else
{
include ($pages_dir . '/home.inc.php');
}
I appreciate any help, feedback or logic and ideas of how to do this right.
Instead of checking which script is running, check $_SERVER['REQUEST_URI']. This will give you the relative URL as requested by the client.

Categories