...

View Full Version : New to PHP and need urgent help!



Machichi
02-16-2006, 08:54 PM
Hi All,

I'm looking to do something like this but in a PHP environment. I just started in PHP and still reading up but this has the get up ASAP. It's a biligual site, and use as a switch. When in english if they want to read the same information they just click spanish. Vice versa from spanish to english.

<%@ LANGUAGE="VBSCRIPT" %>
<%
sString = Request.ServerVariables("HTTP_REFERER")
sFullFileName = Mid (sString, Instr (sString, ".asp"))
iLen = Instr (sString, ".asp")-3
sFileName = Mid (sString, 1, iLen)
sExtension = Mid (sString, iLen+1, 2)
if (sExtension = "en") Then
sExtension = "es"
else
sExtension = "en"
end if
sNewFileName= sFileName & sExtension & sFullFileName
Response.Redirect sNewFileName
%>

Thank you!

StupidRalph
02-17-2006, 01:52 AM
Since you're already coding in ASP, I'd recommend DL the PHP manual.chm. It helped my transition over to PHP.
I won't write the code for this but I'll show you what you'll need to use. Hopefully, it will help you to write it.

ASP

<%@ LANGUAGE="VBSCRIPT" %>
You don't need this above language specification in PHP.
ASP

<% %>

<?php ?>

ASP:

sString = Request.ServerVariables("HTTP_REFERER")

sString = $_SERVER['HTTP_REFERRER'];

ASP

Mid()

substr()
Not exactly identical but you can play with substr() and get what you want.

ASP

Response.Redirect sNewFileName


header(location: $sNewFileName);
exit;

If this isn't enough to get you started let me know.

P.S. strlen() is used to return the string's length.

Machichi
02-17-2006, 03:55 AM
So far... I'm missing something but not sure!
I may just have put up crap also...



<?php
$sString = $_SERVER['HTTP_REFERRER'];
$sFullFileName = substr($sString, strlen($sString, ".php"));
$iLen = strlen($sString, ".php")-3;
$sFileName = substr($sString, 1, $iLen);
$sExtension = substr($sString, $iLen+1, 2);
if ( $sExtension == "en") {
$sExtension = "es";
}else{
$sExtension = "en";
}
$sNewFileName= $sFileName . $sExtension . $sFullFileName;
header(location: $sNewFileName);
exit;
?>

StupidRalph
02-17-2006, 04:13 AM
What error are you getting?

And actually the header parameter should have been a string. Sorry I left that out.



<?php
$sString = $_SERVER['HTTP_REFERRER'];
$sFullFileName = substr($sString, strlen($sString, ".php"));
$iLen = strlen($sString, ".php")-3;
$sFileName = substr($sString, 1, $iLen);
$sExtension = substr($sString, $iLen+1, 2);
if ( $sExtension == "en") {
$sExtension = "es";
}else{
$sExtension = "en";
}
$sNewFileName= $sFileName . $sExtension . $sFullFileName;
header("Location: $sNewFileName");
exit;
?>

Machichi
02-17-2006, 04:29 AM
Warning: Wrong parameter count for strlen() in /home/babbu/public_html/lithium/lang.php on line 3

Warning: Wrong parameter count for strlen() in /home/babbu/public_html/lithium/lang.php on line 4
en

Mhtml
02-17-2006, 04:44 AM
strlen($sString, ".php")-3

Should be


strlen($sString.".php")-3

. concatenates the strings.

StupidRalph
02-17-2006, 04:58 AM
I wonder why I didn't get that parse error....???

Machichi
02-17-2006, 05:03 AM
awesome...

but getting

http://bubba/lithium/en

when it should be http://bubba/lithium/index-es.php

so in my index-en.php I have a link going to lang.php that is this code... I want it to change the en from the file name to es.



<?php
$sString = $_SERVER['HTTP_REFERRER'];
$sFullFileName = substr($sString, strlen($sString.".php"));
$iLen = strlen($sString.".php")-3;
$sFileName = substr($sString, 1, $iLen);
$sExtension = substr($sString, $iLen+1, 2);
if ( $sExtension == "en") {
$sExtension = "es";
}else{
$sExtension = "en";
}
$sNewFileName= $sFileName.$sExtension.$sFullFileName;
header("Location: $sNewFileName");
exit;
?>

StupidRalph
02-17-2006, 05:36 AM
Echo out your 3 variables so we can see what each is outputting. And also, lets comment out the redirect until we get the script working.



echo "sFilename = " . $sFileName . "<br />\n";
echo "$sExtension = " . $sExtension . "<br />\n";
echo "$sFullFileName = " . $sFullFileName . "<br />\n";



<?php
$sString = $_SERVER['HTTP_REFERRER'];
$sFullFileName = substr($sString, strlen($sString.".php"));
$iLen = strlen($sString.".php")-3;
$sFileName = substr($sString, 1, $iLen);
$sExtension = substr($sString, $iLen+1, 2);
if ( $sExtension == "en") {
$sExtension = "es";
}else{
$sExtension = "en";
}
$sNewFileName= $sFileName.$sExtension.$sFullFileName;
//header("Location: $sNewFileName");
//exit;

echo "sFilename = " . $sFileName . "<br />\n";
echo "$sExtension = " . $sExtension . "<br />\n";
echo "$sFullFileName = " . $sFullFileName . "<br />\n";
?>

Machichi
02-17-2006, 12:32 PM
sFilename =
en = en
=

Hmmm this give a good hint

NancyJ
02-17-2006, 12:40 PM
NM - got it now.

NancyJ
02-17-2006, 01:04 PM
The main problem with the above script is you spelled referer wrongly ;)

This is what I think you're trying to do - take the url of a page and replace en with es or vice versa and if neither is found, go to the english page - this is how I would do it



$pageName = $_SERVER['HTTP_REFERER'];

if(substr($pageName, -5, 5)== "-en.php")
{
$newPage = str_replace("-en.php", "-es.php", $pageName);
}
elseif(substr($pageName, -5, 5)== "-es.php")
{
$newPage = str_replace("-es.php", "-en.php", $pageName);
}
else
{
$newPage = str_replace(".php", "-en.php", $pageName);
}
header("Location: $newPage");


If you didnt want to default to english and you knew there was always either -en or -es in the url then you could even shorthand it to:


$pageName = $_SERVER['HTTP_REFERER'];

$newPage = (substr($pageName, -5, 5)== "-en.php") ? str_replace("-en.php", "-es.php", $pageName) : str_replace("-es.php", "-en.php", $pageName);

header("Location: $newPage");

Machichi
02-17-2006, 01:18 PM
Working for -es.php page to -en.php page but not the other way arround.

Thank for your help people BTW

NancyJ
02-17-2006, 01:23 PM
which version are you using?

Do you have a link to a live version so we can see it?

Machichi
02-17-2006, 01:33 PM
http://lithium/index-es.php?l=3&c=e0

While using your code...



$pageName = $_SERVER['HTTP_REFERER'];

$newPage = (substr($pageName, -5, 5)== "-en.php") ? str_replace("-en.php", "-es.php", $pageName) : str_replace("-es.php", "-en.php", $pageName);

header("Location: $newPage");

NancyJ
02-17-2006, 01:38 PM
$pageName = $_SERVER['HTTP_REFERER'];

$newPage = (strstr($pageName, "-en.php")!==false) ? str_replace("-en.php", "-es.php", $pageName) : str_replace("-es.php", "-en.php", $pageName);

header("Location: $newPage");

Machichi
02-17-2006, 01:41 PM
Thank you! :thumbsup:

Wish we could send a jug of beer by internet.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum