PHP-Session help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

PHP-Session help

Post 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.
anotherphpnoob
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 12:13 pm

Re: PHP-Session help

Post 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.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post by Aravinthan »

I do run session_start(),

In both of my functions....
anotherphpnoob
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 12:13 pm

Re: PHP-Session help

Post by anotherphpnoob »

oh sorry didn't see that, i was looking for it at the very beginning of the code.
bibumathew
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 5:36 pm

Re: PHP-Session help

Post 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]
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post 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
anotherphpnoob
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 12:13 pm

Re: PHP-Session help

Post 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.
bibumathew
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 5:36 pm

Re: PHP-Session help

Post by bibumathew »

what version of php are u using ?
bibumathew
Forum Newbie
Posts: 8
Joined: Tue Aug 03, 2010 5:36 pm

Re: PHP-Session help

Post 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);
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post by Aravinthan »

5.2.13
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post 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");

?>
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post by Aravinthan »

UP
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: PHP-Session help

Post by Aravinthan »

Anyone ?
Post Reply