php menu and multilanguage page - php

I am developing a multi-language website with php. I used session to take language change request from users.
<?php
$check_lang=array("eng","suo","sve");
session_start();
if(isset($_GET["lang"])){
if(in_array($_GET["lang"], $check_lang)){
$_SESSION["lang"]=$_GET["lang"];
}
}
if($_SESSION["lang"]==""){
$_SESSION["lang"]="eng";
}
include("Lang/".$_SESSION["lang"]."/".$_SESSION["lang"].".php");
?>
This works pretty well. But the problem is that once the user navigates different menu(say "About" in English) and presses a link to change language to Swedish, the page redirects to "home" in Swedish.
I would like to know how to record which page user is currently and change that particular page on language change request.
-thanks on advance.

You can use $_SERVER["HTTP_REFERER"] to see where people are coming from. People can actually change this so make sure you build some security so people don't get redirected to other domains etc.

Related

LARAVEL Redirect user to same page they are viewing when language change link is clicked in the web page

I am new to Laravel and the scenario is like this: I have a bilingual site built using laravel and my problem lies in let's say, for example, the user is viewing about us page and he chose to change language of the about us page to french. Now what i want to do is redirect the user to the same page url (about us) page even after language change link is clicked. And my language changing logic is based on setting session variable i.e. if sesssion[$lang]= 1 fetch english content and if 2 fetch french content. My daunting problem lies in capturing the currently active page URL and redirect the user to the same URL after changing the language to french !!!!! And i don't even know is my change language logic standard one or not
An easy way is to use the redirect()->back() method to go back where you were , but i believe a better practice is to define language also through routes example
Route::get('about_us/{lang}','YourController#method_name');
and grouping them by middleware to switch between languages and redirecting to about_us/ar , about_us/en or whatever but you have mentioned that you are using sessions so just
redirect()->back()
after changing language .

Getting page language and redirecting to same language page via polylang

I'm using polylang to translate my wordpress website and it's working fine until now...
The default language is english. Let's say I'm in a page in french, with a link to another page. I'd like it to be linked to that other page in french.
<a href="http://localhost:8888/lacolmena/a-propos/" class="apropos">
You'd say just add /fr at the end of the link. The thing is, I want it to detect which language is currently on and redirect the link to the equivalent language for the page I'm looking to get to.
Using Polylang, I managed to get all of my pages translated and working smoothly. I have a functional menu that allows me go to anywhere I want to go in any language. Only one link, in one page is causing me problems because it doesn't know if it could go the /fr or /en.
So I need to find a way to get if the current link if /fr or /en and redirect to the page in the equivalent language.
Basically :
my page is in french
go to the french contact page
or
my page is in english
go to the english contact page
I have no idea how to get to that. Should I be using PHP ? I've never used PHP before, so that's kind of creeping me out, even though I'm willing to make an effort to learn it.
I've seen this Polylang function while researching, perhaps it could be a beginning for my algorithm ?
pll_current_language($value);
I'm not sure my description is clear.. I'm really lost here :/
setup
<?php get_the_permalink(pll_get_post(get_page_by_path( 'page-slug' )->ID));?>
url path
URL TEXT
Try
get_the_permalink(pll_get_post(get_page_by_path( 'page-slug' )->ID));
It could look something like this
Contact

How to resolve multilanguaes for social bots?

I have done multi languages on my web site.
When user visits page at first time works mechanism that detects country and language.
After is occurring reloading page and data is added to current session.
Problem is that when I try to share links in social networks I get empty data, because social bot does not have session mechanism therefore web server returns empty page without text.
How I can resolve this issue or may be change something in architecture.
To share please a good prartic about this.
Don't use sessions for this. If you want to share the page URL with the right language, make sure this language is part of the URL. A simple way to do this is to use a query string. Your URL could look like this: http://www.example.com/page?language=dutch
In PHP you can use $_GET to read which language to use:
<?php
if ( isset( $_GET['language'] ) ) {
// Display page in specified language
} else {
// Display page in default language
}

How can I place a photo on someone else's webpage when linked to from my site?

I know this is a strange thing to want to do, but...
How can I place a photo at the top of someone else's webpage ("Page B") -only- when a visitor links there from my page ("Page A")? I have permission to do this, but with certain limitations: my code needs to be in one location on Page B, and I can't make major changes to Page B. I cannot use Page B's server to run PHP, but I can run PHP on Page A's server.
The photo needs to open automatically with the rest of the Page B, and not require any user input to show up.
This is a unique vendor/retailer situation, so unfortunately I can't provide the urls.
Thanks for any help.
In PHP, you'd do something like:
<?php
if($_SERVER['HTTP_REFERER'] == 'http://www.yoursite.com/referringpage.html')
{
printf('<img src="yourbutton.png">');
}
?>
Which is very limited because:
It will only work for a referring single page (referringpage.html) and not otherreferringpage.html
It will only work for a single landing page. If the user navigates off of the page, then back (not through browser's back), the button will not show up.
To solve the first you could do a substring of the referrer, e.g.
<?php
if(substring($_SERVER['HTTP_REFERER'],0,24) == 'http://www.yoursite.com/')
{
printf('<img src="yourbutton.png">');
}
?>
To solve the second you could set session variables (except then you'd need access to the very top of the "outermost" page -- which doesn't sound like an option.)
Depends on the design of the page.
Lets say,
If the page is divided into frames, then you can use iframe.
if It's a single page, try have a separation with div tag. Insert the html into Div Tag.
If the above two points won't help you,give an idea of how the page is designed
I think a simple answer would be to write a html page (lets call it linkPage) that has the image/text/div or whatever you want to display on the top and then include an iframe that would cover the rest of the page using css. You could pass the page you are linking to as a GET or POST parameter to the linkPage and then set the src of the iframe to that url.
There are probably other ways of doing this that are more complicated / seamless but this would be the easiest, quickest way to get it done.

PHP: check from which page the link is directed

lets say i have a page hit.php in my website and the link is to this page is available on every\many pages in the website..
lets say i click <a href"hit.php">HIT</a> from any page.....
is there a way when hit.php is loaded i could know that the page from which this page was..
meaning if i click the link to hit.php from 'index.php' i could check loading hit.php that the user is navigating here from 'index.php' or any other page...
i know this i could establish in site by passing variables in URL like 'hit.php?pagename=index' but is there another way
p.s i know its crazy but still :)
$_SERVER['HTTP_REFERER'], although it can easily be spoofed or disabled.
You can use the referer. In PHP, it's:
$_SERVER['HTTP_REFERER']
However, users can configure their browsers not to send this, or spoof it. You should ensure that your code doesn't break if it's absent, or faked.

Categories