/    Sign up×
Community /Pin to ProfileBookmark

Passing a variable to the same page as it reloads

I’m trying to make one menuitem go to a page based upon whatever page is currently loaded.
E.g.

if
index.php?page=mypage&lng=no
is loaded, I want to set the menuitem to go to

index.php?page=mypage&lng=en
if
index.php?page=yourpage&lng=no
is loaded, I want to have the same menuitem to go to

index.php?page=yourpage&lng=en

I then use

include $lng.”_”.$page.”.php”;

to select the page to be included, so what I really what to do is to preserve the $page value.

I use phplayersmenu ( [url]http://phplayersmenu.sourceforge.net/[/url] )

Regards
The BluesMan

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMay 19.2005 — try[code=php]<?php
$page = (isset($_GET['page']) && $_GET['page'] == 'mypage') ? 'yourpage' : 'mypage';
$lang = (isset($_GET['lng']) && !empty($_GET['lng'])) ? $_GET['lng'] : 'en';
echo "<a href="index.php?page=$page&amp;lng=$lang">link</a>";
?>[/code]
Copy linkTweet thisAlerts:
@BluesManauthorMay 19.2005 — Thanks a lot for your reply.

It's a nice try, but it won't work, unfortunately. If you go to http://mathisenonline.com/bhm/index.php?lng=en and play around with the url, you just might get a better idea of what I want to do. You may switch languages in the submenu under Hjem / Home, but that always takes you to the start-page.

You see that I have a few valiables in the url: d, page and lng.

d= directory (default is files)

page= include-filename without extention

lng= language

The included file will be $lng."_".$page.".php"

Translated that is en_filename.php

or, in norwegian, no_filename.php

As you probably gather by now, I want to change the lng from no to en (and vice versa in the English menu), and keep all the rest.

I thought it would be easyest to set lng to it's new value directly in the menu and pass the $page value to index.php as it reloads. There's got to be some way to get and remember the value and pass it to a variable or something in the reloaded index.php, and then just, in the url, set page=foo and check

if ($page="foo") { $page=variable_with_the_passed_value; }

Perhaps there is a smarter way to do it, I don't know, I'm all ears.

Perhaps I coulds use something with $_POST ?

Would I need a form, or could the script itself use $_
POST ?

If it is any help, here is the code I use to get the variables from the url:

if (isset($_GET['d'])){

$D=$_
GET['d'];

}else{

$D="files";

}

if (isset($_GET['page'])){

$page=$_
GET['page'];

}else{

$page="start";

}

if (isset($_GET['lng'])){

$lng=$_
GET['lng']."_";

}else{

$lng="no_
";

}

And here is the code I use to include the file:

if (file_exists($pth.$D."/".$lng.$page.$ext1)) {

include $pth.$D."/".$lng.$page.$ext1;

}else{

if (file_exists($D."/".$page.$ext1)) {

include $D."/".$page.$ext1;

}else{

echo "Page not found.<br>Siden finnes ikke.";

}

}

Never mind the $pth variable - I use it to take care of the scopes. It's a whole different story, it works and we can forget about that now.

$ext1 is set to .php

Gives me the possibility to use $ext2 =".txt" or something - another thing that's needless to worry about.

Kind regards

The BluesMan
Copy linkTweet thisAlerts:
@BluesManauthorMay 20.2005 — Anyone have an idea about how to do this? I just can't figure it out and it drives me crazy.

Perhaps I should start updating myself on sessions (I'm really not very skilled with php)? I'm sure I can do this with sessions, but I have no idea about how to use sessions (yet), so I was hopeing there was a rather simple way of passing a variable through a reload (should be the same as passing it to another page, shoulden't it?).

Kind regards

The BluesMan (now I really got the Blues ... ?)
Copy linkTweet thisAlerts:
@BluesManauthorMay 24.2005 — I knitted together a solution myself. It's not very elegant, but it seems to work.

I use this url in the menu when I want to switch languages:

index.php?trans=en

to switch to English, and obviously

index.php?trans=no

to switch to Norwegian

In index.php I have this code to make the switch:

<?php

$x = explode ("&",$_SERVER['HTTP_REFERER']); // Get the different parts of the url you came from

if (isset($_
GET['trans'])){ // If trans i set in the url,

$D=preg_replace("/d=/","",$x[1]); // set $D to the value of d in the url you came from, e.g. files instead of d=files

$page=preg_replace("/page=/","",$x[2]); // set $page to the value of page in the url you came from

if (file_exists($pth.'menu/mnu/'.$_GET['trans'].'_hovedmeny.mnu')){ // Check if the menustructurefile exists

$lng=$_GET['trans']; // If it does, $lng is set to the value of trans in the url you came from

}else{ // If not

$lng="en"; // $lng is set to English as default

}

$query = "&".$x[1]."&".$x[2]."&lng=".$lng; // I don't really need this, but it saved me some typing. It sets $query to the querystring in the url you came from, except for lng, which is set to the value of trans (if index.php?trans=no , then no will be the value here)

header("Location: http://mathisenonline.com/bhm/index.php?".$query); // Reload the page to preserve the refering url (the one you came from)

}

?>

There are a few more details that have to be in the index.php to make sure it works properly, but basicly, this is it.

Anyone have a more elegant solution, I'm all ears.

Kind regards

The BluesMan
Copy linkTweet thisAlerts:
@DaiWelshMay 24.2005 — Seems to me that you can just do (untested)

$trurl = preg_replace('/[&|?]lang=[^&]*/','',$_SERVER['PHP_SELF']);

$trurl .= (strcmp($lng,'no_
')?'no_':'en_');

then use $trurl as the href on your translate link. This should retain the full url except that the the language param(if present) is stripped out and replaced with the 'other' language (the one that the current page is not using).

Getting the link url right is going to be a lot more reliable than trying to correct things on the target page as the http_referer can not be relied on.

HTH,

Dai
Copy linkTweet thisAlerts:
@BluesManauthorMay 24.2005 — Nice, DaiWelsh. I'll try that. But, how do I add more languages? In a not too near future, I hope to add Thai as well. Oh, well, came to think of it - phplayersmenu cant use thai fonts, unless I use a database. On my homepage I just may do that, but there are a couple of other sites that don't have databases. Now, these sites are not likely to add Asian languages, but, perhaps German or Spanish in addition to Norwegian and English.

Anyway, I totally agree with you that the http_referer can not be trusted, and besides, your way means a lot less code.

Thanks, mate, I'll give it a try.

Kind regards

The BluesMan
Copy linkTweet thisAlerts:
@BluesManauthorMay 24.2005 — Just came to think of it ... exactly why would the http_referer be unreliable in this case?

It will always be the url you are at when you click on the language button. The php_self/query_string in the script will always be the next page you arrive to (the page that will be loaded when you click on the language button).

I have tried over and over, but I can't find a way to make the script fail (there are ways to get an error message that says "File not found. Siden finnes ikke.", but that is normal. You will alwaays get this message if an include page is not found. Click on a link and you're back in business. There may also be pages that are not yet translated into English. In that case

you will also get this error message. Click on the language button, and you're back to the Norwegian page, even if the English page doesn't exist.

Anyone who can make the script fail, please let me know and I'll try to correct it.

Go to http://mathisenonline.com/ and play around with the menus and even fiddle with the url of all your heart.
Copy linkTweet thisAlerts:
@DaiWelshMay 24.2005 — http_referer is passed by the browser and is not a required part of the HTTP spec. Some browsers will not pass it but far more commonly some security/privacy products such as norton internet security will deliberately strip it out, which means any code using it will not work for users of these products. PHP_SELF is set by php on the server and will always work regardless of anything the browser may do.

HTH,

Dai
Copy linkTweet thisAlerts:
@BluesManauthorMay 24.2005 — Thank you, I didn't know that. All the more reason to try out your suggestion ?

I just knew I wasn't that clever ...

We'll see tomorrow as it's now midnight in Oslo.

Take care.
Copy linkTweet thisAlerts:
@BluesManauthorMay 25.2005 — $trurl = preg_replace('/[&|?]lang=[^&]*/','',$_SERVER['PHP_SELF']);

$trurl .= (strcmp($lng,'no_
')?'no_':'en_');[/QUOTE]


Now I have had a closer look at this code, and I'm sorry, but I fail to see how this will get

?d=files&page=links&lng=no to become

?d=files&page=links&lng=en ?

If I only could put ?d=files&page=<?php echo $page; ?>&lng=en

in the menuitem itself, but I'm using [URL=http://phplayersmenu.sourceforge.net]phplayersmenu[/URL] and it is not possible to put php-code in the menu structure file, so I have to think of somthing else. According to the author, it is possible to do what I want by changing some settings in the configuration, but I'm not so sure.<br>

Anyway, go to http://seksan.mathisenonline.com to see what I want and play with links and url. The page will look for d, page, lng and trans in the url. Anything else is disregarded.<br>
×

Success!

Help @BluesMan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...