Page 1 of 1

PHP-Session help

Posted: Tue Aug 03, 2010 5:43 pm
by Aravinthan
Hi guys,

Ok so what I need to do is change the Database name using session.

The admin has to update 2 database, one after another, so I tought of sessions.

But it doesnt work. So here is the code:

Code: Select all

<?php

$ligue = $_GET['ligue'];

function redirect_to($location = NULL) {
  if ($location != NULL) {
    header("Location: {$location}");
    exit;
  }
}
function is_connected() {
	session_start();
	if(isset($_SESSION['logged'])) {
		return true;
	} else {
		redirect_to("/uw-ligues/admin/login.php");
	}
}

function setLeague() {
	session_start();
	$_SESSION['ligue'] = $ligue;
	$var = $_SESSION['ligue'];

}

is_connected();
setLeague();


if($var != "") {
$chosed =  $var;
} else {
$chosed = "ligue1";
}

echo "Ligue: $chosed<br/>";

$link = mysql_connect ("localhost", "user", "pass")or die("mysql_error()");



		mysql_select_db ("DB_$chosed", $link)or die("mysql_error()");

		?>
So, as you guessed it, the $ligue is sent trought a GET variable from a previous link.

Re: PHP-Session help

Posted: Tue Aug 03, 2010 5:59 pm
by anotherphpnoob
i am having problems with sessions too. In Aravinthan's case id say it was because he didnt use session_start(); before hand, but for me i am using it and my session variables are all not set and empty.

Re: PHP-Session help

Posted: Tue Aug 03, 2010 6:00 pm
by Aravinthan
I do run session_start(),

In both of my functions....

Re: PHP-Session help

Posted: Tue Aug 03, 2010 6:06 pm
by anotherphpnoob
oh sorry didn't see that, i was looking for it at the very beginning of the code.

Re: PHP-Session help

Posted: Tue Aug 03, 2010 6:28 pm
by bibumathew
Hope this will work for you, if you have any queries please let me know

Code: Select all

<?php

    $ligue = $_GET['ligue'];
    $var=NULL;

    function redirect_to($location = NULL) {
      if ($location != NULL) {
        header("Location: {$location}");
        exit;
      }
    }
    function is_connected() {
       session_start();
       if(isset($_SESSION['logged'])) {
          return true;
       } else {
          redirect_to("/uw-ligues/admin/login.php");
       }
    }

    function setLeague() {
    	//added global 
    	global $ligue,$var;
       session_start();
       $_SESSION['ligue'] = $ligue;
       $var = $_SESSION['ligue'];

    }

    is_connected();
    setLeague();


    if($var != "") {
    $chosed =  $var;
    } else {
    $chosed = "ligue1";
    }

    echo "Ligue: $chosed<br/>";

    $link = mysql_connect ("localhost", "user", "pass")or die("mysql_error()");



          mysql_select_db ("DB_$chosed", $link)or die("mysql_error()");

          ?>
[/color]

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:04 pm
by Aravinthan
Hi,

Thanks for your help, but still nothing...

Oh and I forgot to mention that I get this warning:
Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0

But no, it still keeps ligue1, instead of switching it to ligue2

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:05 pm
by anotherphpnoob
this didn't help me. here is how I am initializing the session variables.

Code: Select all

      foreach ($row as $val)
	  {  
	    $_SESSION[$rowcount] = $val;
//		echo "<td>$val</td>\n";
		$rowcount=$rowcount+1; 
      }
they should be set because i checked them later on from the same php page. after the new web page loads all of the session variables are just not set. Should i be using post and get like Aravinthan? if there is an easier way to pass variables between pages id love to hear them.

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:34 pm
by bibumathew
what version of php are u using ?

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:37 pm
by bibumathew
Can you please paste this in the beginning of ur code

and run it again


ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:40 pm
by Aravinthan
5.2.13

Re: PHP-Session help

Posted: Tue Aug 03, 2010 7:42 pm
by Aravinthan
It took the warning away,

But it still doesnt switch the session variables....

Here is the page I use to set the datas for the $GET:

Code: Select all

<?php
include("fichiers/connection.php");

include("style/header.php");

echo "Admin Cpanel</br>";

echo "1- <a href='fichiers/index.php?ligue=ligue1'>Ligue 1</a><br/>";

echo "2- <a href='fichiers/index.php?ligue=ligue2'>Ligue 2</a><br/>";


include("style/footer.php");

?>

Re: PHP-Session help

Posted: Wed Aug 04, 2010 8:15 am
by Aravinthan
UP

Re: PHP-Session help

Posted: Thu Aug 05, 2010 9:08 am
by Aravinthan
Anyone ?