Welcome Guest ( Log In | Register )

Bump Topic Topic Closed RSS Feed

Outline · [ Standard ] · Linear+

PHP Unable to pass a session variable to another page

views
     
TSMrLabuLabi
post Feb 4 2016, 03:21 PM, updated 9y ago

New Member
*
Junior Member
42 posts

Joined: Sep 2014
Hello blush.gif

FYI, im using Joomla CMS as my website platform

I want to redirect my visitor to different website based on their respective country. (GeoIP)

malaysia -> www.myweb.com/my
thailand -> www.myweb.com/th
indonesia -> www.myweb.com/id

In order to do so, i'm trying to use "session" function to detect my visitor country so that the can be be redirected to correct website.

My testing script as below.

index.php

CODE

<?php
session_start();

$ip = $_SERVER['REMOTE_ADDR'];
$url = "http://www.geoplugin.net/xml.gp?ip=".$ip;

$xml = simplexml_load_file($url);
//echo $xml->geoplugin_countryName;

$country= $xml->geoplugin_countryName;

if ($country == "Malaysia") {

echo ' MY:<br>';
echo $country;
echo '<br>';

$_SESSION["country"] = $country;
echo  $_SESSION["country"];

?>
<script type="text/javascript">
window.location = "page.php";
</script>  
<?php

elseif ($ctr == "Thailand") {
 echo ' thailand';
}

else {
 echo $xml->geoplugin_countryName;
}

?>



This is the output for index.php

MY:
Malaysia
Malaysia

However, when the page has been redirected to page.php, the "session' has return NULL (blank) value. Please advise

page.html

CODE

<?php
session_start();

echo 'test script';

echo  $_SESSION["chk"];

?>


Perhaps is there any other solution for this problem?
alien3d
post Feb 4 2016, 04:19 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009

serious beserabut aku baca
CODE

         session_start();
         $ip = $_SERVER['REMOTE_ADDR'];
         $url = "http://www.geoplugin.net/xml.gp?ip=".$ip;
         $xml = simplexml_load_file($url);
         $_SESSION["country"]= $xml->geoplugin_countryName;
         if($_SESSION["country"]!="malaysia") {
           header("location:page.php");
           exit();
         }  

Other page
CODE

session_start();
var_dump($_SESSION);


This post has been edited by alien3d: Feb 4 2016, 04:19 PM
TSMrLabuLabi
post Feb 5 2016, 09:49 AM

New Member
*
Junior Member
42 posts

Joined: Sep 2014
QUOTE(alien3d @ Feb 4 2016, 04:19 PM)
serious beserabut aku baca
CODE

         session_start();
         $ip = $_SERVER['REMOTE_ADDR'];
         $url = "http://www.geoplugin.net/xml.gp?ip=".$ip;
         $xml = simplexml_load_file($url);
         $_SESSION["country"]= $xml->geoplugin_countryName;
         if($_SESSION["country"]!="malaysia") {
           header("location:page.php");
           exit();
         }  

Other page
CODE

session_start();
var_dump($_SESSION);

*
output still NULL sweat.gif
alien3d
post Feb 5 2016, 12:06 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(MrLabuLabi @ Feb 5 2016, 09:49 AM)
output still NULL  sweat.gif
*
1. Use editor like zend studio / phpstorm..
2. no empty space <? in html when using session_start
3. Check your php.ini enable whatever library require maybe xml

CODE

session_start();
        $ip = $_SERVER['REMOTE_ADDR'];
        $url = "http://www.geoplugin.net/xml.gp?ip=".$ip;
        $xml = simplexml_load_file($url);
        if($xml) {
         $_SESSION["country"]= $xml->geoplugin_countryName;
         } else{
             echo "something wrong with this function dear ";
         }
         if($_SESSION["country"]!="malaysia") {
          header("location:page.php");
          exit();
        }


WongGei
post Feb 5 2016, 12:55 PM

Regular
******
Senior Member
1,206 posts

Joined: Dec 2007
From: Kuala Lumpur
Have you try mod_geoip?

The country will be in $_SERVER[GEOIP_COUNTRY_CODE]
chriss2010
post Mar 29 2016, 06:53 AM

Getting Started
**
Junior Member
71 posts

Joined: Sep 2009
1. null as the xml retrieved as object cannot be passed into session, do typecasting to string then store to php session.
2. since the xml has the country code (might not be the same as your url country prefix though...) use php's header location for redirection.




CODE

$ip = $_SERVER['REMOTE_ADDR'];
$url = "http://www.geoplugin.net/xml.gp?ip=".$ip;

$xml = simplexml_load_file($url);

$_SESSION['country'] = (string)$xml->geoplugin_countryName;
$_SESSION['country_code'] = (string)$xml->geoplugin_countryCode;

header("location: http://www.myweb.com/{$_SESSION['country_code']}");


Topic ClosedOptions
 

Change to:
| Lo-Fi Version
0.0115sec    0.41    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 12:44 PM