PHP session variable

Discussion in 'PHP' started by chxxangie, Dec 10, 2007.

  1. #1
    at login page, i create a new session variable named active

    
    $_SESSION['active']=1;
    
    PHP:
    at the following page, i try to call out the sesion variable but failed.
    
    <?
    session_start();
    echo $_SESSION['active'];
    ?>
    
    PHP:
    it is weird. coz last few days the website is running well but now face this kind of problem....
    cannot do login event totally...
    it cannot echo the "1"...the result come out is just empty....
    please help me....thanks........
     
    chxxangie, Dec 10, 2007 IP
  2. Xariov

    Xariov Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't really see what's the problem.. It shouldn't be anything, should it? :eek:
    Sessions isn't actually for echoing.. If you want to print a "1" use variables instead?
    This is how you could do it with sessions:
     
    Xariov, Dec 11, 2007 IP
  3. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not quiet sure what you mean here Xariov...

    You can use session variables for anything you like and are especially useful for echoing their values, such as the logged in user's real name for example.
     
    Gawk, Dec 11, 2007 IP
  4. Xariov

    Xariov Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, if he only wish to print a "1" then I would've suggested variables or just echo "1";
     
    Xariov, Dec 11, 2007 IP
  5. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Xariov - OK, I see what you were trying to say now ;)

    chxxangie - Check that you have session_start() at the top of the page you set $_SESSION["active"] to 1 - it probably is but you didn't show it on your code snippets ;)

    Has you service provider upgraded the server with a newer version of PHP or changed the default values?
     
    Gawk, Dec 11, 2007 IP
  6. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ya, i'd already put session_start() at the top of the page. but the problem is still there.
    i'm using phpdev423. before today... all is running well. but now,
    the session active variable cannot pass to other pages.
     
    chxxangie, Dec 11, 2007 IP
  7. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Are all other session variables working? Have you tested to see if you can set another session variable and see if that passes to the next page?
     
    Gawk, Dec 11, 2007 IP
  8. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    login.php page

    
    <?php
    	session_start();
    	$_SESSION['active']=1;	
    ?>
    	<script type="text/javascript">
    	window.location="choose.php"
    	</script>
    	<? 
    
    PHP:


    choose.php page

    
    <?
    
    session_start();
    	echo "current session is: ".$_SESSION['active'];
    	if(!$_SESSION){ 
    
    ?>
    	<SCRIPT LANGUAGE="Javascript">		
    		window.location = "login.php";
    	</SCRIPT>
    <?php
    	exit;
    	}
    ?>
    
    
    PHP:
    any problem of above code?
    or is there anyway can replace the session so that i can pass the variable among pages?
     
    chxxangie, Dec 11, 2007 IP
  9. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The script looks fine, it worked for me.

    Is it all session variables that don't work, do you set others and can you test them by echoing them to the screen?
     
    Gawk, Dec 11, 2007 IP
  10. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ya...all variable don't work...
    i'd tried to create new session and echo to screen but session value is empty...
    i don't know what had happened? infected of virus??? but is it the virus is so clever to infect only the session???
    or because of the localhost program? may be...coz i'd tried to upload the files to server, session is works.
     
    chxxangie, Dec 12, 2007 IP
  11. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You are not blocking your own cookies are you?
     
    Gawk, Dec 12, 2007 IP
  12. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    no...i never do that.
    i use phpdev currently...and when i tried to use Wamp server, more problem arised...
    really make me headache...
     
    chxxangie, Dec 12, 2007 IP