PHP 5.2 sessions

Ask about a PHP problem here.
Post Reply
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

PHP 5.2 sessions

Post by Helx »

Wow, PHP 5.2 is really annoying XD

Right, so I have this code:
<?php
	  session_start();
      if(isset($_SESSION['user'])) {
		  $session_username = $_SESSION['user'];
		  ?>
      <img src="<?php echo "http://website.net/render.minecraft.face.php/?player=" . $session_username ?>" height="20" width="20" /> <font color="#666666" style="padding-left:6px"><?php echo $_SESSION['username']; ?></font> // EDIT: lol, I put in the actual website
      <?php
	  }else{
	  ?>
      <font color="#666666"><a href="/login">Login</a></font>
      <?php } ?>
And it will only show the }else{ part EDIT: (after logging in). *irked*

This is the login script:
<?php
	
	include('core/DB_connect.php');
	$user=$_POST['user']; 
	$password=$_POST['password'];
	$user = stripslashes($user);
	$password = stripslashes($password);
	$user = mysql_real_escape_string($user);
	$password = mysql_real_escape_string($password);
	$enc=md5($password);
	$sql="SELECT * FROM $tbl_name WHERE username='$user' and password='$enc'";
	$result=mysql_query($sql);
	$count=mysql_num_rows($result);
	if(isset($_POST['user'])){
		if($count==1){
			session_start();
			$_SESSION['user'] = $_POST['user'];
			header("Location: ../");
		}else{
			$false_enc = '<b><font color="#CC0000">Incorrect details</font></b>';
			$enc_type = 'text-input-wrong';
		}
	}else{
		$false_enc = 'Need help? Contact the <a href="/contact/admin/">Admin</a>';
		$enc_type = 'text-input';
	}
?>
And the logout script (just in case)
<?php
session_start();
unset($_SESSION['user']); // Its the only thing set, so I'm not going to unset the pass.
header('Location: http://website.net/'); // EDIT: lol, I put in the actual website
?>
I would really appreciate any help, I'm stumped :/
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP 5.2 sessions

Post by jacek »

Hmm, well there is no point in defining this variable
$session_username = $_SESSION['user'];
you may as well just use the $_SESSION['user'] one directly.

But if you are always getting the else part that must mean that isset() is always returning false so you could try adding a
print_r($_SESSION);
above that to see what is actually in the session.
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP 5.2 sessions

Post by Helx »

print_r($_SESSION); returns "Array ( )"

I'm guessing that means its got something to do with validation?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP 5.2 sessions

Post by jacek »

Maybe the session is never starting on the login page but it's being hidden from you because of the redirect ?

Try commenting out the header("Location: ../"); line and see if an error appears when you log in.
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP 5.2 sessions

Post by Helx »

Still returning the same thing sadly...

Is there anything that needs to be explicitly defined for $_SESSION to work?
Something like the older versions, where session_start() was required? (I do have session_start() in the code.. Might that be the reason?)

EDIT:

Ah, ok. I think it may be something to do with PHP itself...
http://forums.phpfreaks.com/index.php?topic=118070.0
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP 5.2 sessions

Post by jacek »

abcedea wrote:Something like the older versions, where session_start() was required? (I do have session_start() in the code.. Might that be the reason?)
Yeah all you need is the session_start() and a browser that will accept the cookie that it sends.
abcedea wrote:Ah, ok. I think it may be something to do with PHP itself...
http://forums.phpfreaks.com/index.php?topic=118070.0
No, that is from 2006 ;)

Do you definitely have error_reporting turned on ? :? Everything looks like it should work really.
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP 5.2 sessions

Post by Helx »

jacek wrote:Everything looks like it should work really.
Thats the thing getting me stuck, I see no problems. Oh, and I turned error_reporting(); to "E_ALL"... Is that right?
When I do that, the Array ( ) still stands..

Okay, I think I'll paste the (almost) entire website here I guess...

Index.php (home page, auth not REQUIRED)
<?php
include('core/init.inc.php'); // This is for the template system... Don't worry about it.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>StratusCraft - RPG Survival</title>
<meta name="description" content="Creative, Survival, Awesome.">
<meta name="author" content="Bleeding Edge Media">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/flexslider.css" type="text/css" media="screen" />
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/reset.css"/>
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraf ... grey.css"/>
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/dark.css"/>
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/style.css"/>
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/skeleton.css">
<link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraf ... yPhoto.css">
<script src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
<script src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
<script src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
<script src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
<script src="http://cdn.stratuscraft.net/StratusCraf ... "></script>
<script src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
<script src="http://cdn.stratuscraft.net/StratusCraf ... "></script>

<!--[if IE 7 ]>
		<link rel="stylesheet" type="text/css" media="all" href="http://cdn.stratuscraft.net/StratusCraft/ie7.css" />
		<script type="text/javascript" src="http://cdn.stratuscraft.net/StratusCraf ... "></script>
	<![endif]-->
<!--[if IE 8 ]>
		<link rel="stylesheet" type="text/css" media="all" href="http://cdn.stratuscraft.net/StratusCraft/ie8.css" />
		<script type="text/javascript" src="http://cdn.stratuscraft.net/StratusCraf ... "></script>
	<![endif]-->
<!--[if lt IE 9]>
		<script src="http://html5shim.googlecode.com/svn/tru ... "></script>
	<![endif]-->

<link rel="shortcut icon" href="http://cdn.stratuscraft.net/StratusCraft/favicon.ico">
<link rel="apple-touch-icon" href="http://cdn.stratuscraft.net/StratusCraft/favicon.ico">
<link rel="apple-touch-icon" sizes="72x72" href="http://cdn.stratuscraft.net/StratusCraf ... %20x72.ico">
<link rel="apple-touch-icon" sizes="114x114" href="http://cdn.stratuscraft.net/StratusCraf ... 20x128.ico">
<link href="http://fonts.googleapis.com/css?family=Abel" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="header">
  <div class="headerbar">
    <div class="container">
      <div class="socialicons"></div>
      <?php
	  session_start();
      if(isset($_SESSION['user'])) {
		  ?>
      <img src="<?php echo "http://cdn.stratuscraft.net/StratusCraf ... p/?player=" . $_SESSION['user'] ?>" height="20" width="20" /> <font color="#666666" style="padding-left:6px"><?php echo $_SESSION['user']; ?></font>
      <?php
	  }else{
	  ?>
      <a href="/login"><font color="#666666">Login</font></a>
      <?php } ?>
    </div>
  </div>
  <div class="line_main"></div>
  <div class="container">
    <h1 id="logo"><a href="?pg=index" title="Homepage"><img src="http://cdn.stratuscraft.net/StratusCraft/logo.png" alt="StratusCraft" /></a></h1>
    <div class="navwrap">
      <div class="nav" id="navi">
        <ul class="menu" id="menu-navi">
          <li class="menu-item">
            <div class="navhover"></div>
            <a href="?pg=index" title="Frontpage">Home<span class="subtitle"> Frontpage</span></a></li>
          <li class="menu-item">
            <div class="navhover"></div>
            <a href="http://forum.stratuscraft.net" title="Be A Member">Forum<span class="subtitle"> Be A Member</span></a></li>
          <li class="menu-item">
            <div class="navhover"></div>
            <a href="appeal.php" title="Appeal Bans">Appeal<span class="subtitle"> Appeal Bans</span></a></li>
          <li class="menu-item">
            <div class="navhover"></div>
            <a href="?pg=donate" title="Get Perks">Donate<span class="subtitle"> Get Perks</span></a></li>
          <li class="menu-item">
            <div class="navhover"></div>
            <a href="?pg=join" title="Server Help">More<span class="subtitle"> Server Help</span></a>
            <ul class="sub-menu">
              <li class="menu-item">
                <div class="navhover"></div>
                <a href="?pg=vote">Vote<span class="subtitle"></span></a></li>
              <li class="menu-item">
                <div class="navhover"></div>
                <a href="/shop/">BlockShop<span class="subtitle"></span></a></li>
              <li class="menu-item">
                <div class="navhover"></div>
                <a href="http://forum.stratuscraft.net/viewtopic ... Rules<span class="subtitle"></span></a></li>
              <li class="menu-item">
                <div class="navhover"></div>
                <a href="?pg=commands">Commands<span class="subtitle"></span></a></li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
    <div class="clear"></div>
  </div>
</div>
<div class="slidew">
  <div class="container">
    <div class="slidew"> 
      <script type="text/javascript" src="http://cdn.stratuscraft.net/jQuery/jque ... "></script> 
      <script type="text/javascript">
	jQuery(window).load(function() {
		jQuery('#slider').nivoSlider({
			effect:'fade',
			animSpeed:600,
			pauseTime:5000,
			startSlide:1,
			directionNav:true,
			directionNavHide:false,
			controlNav:true,
			controlNavThumbs:true,
			controlNavThumbsFromRel:true,
			controlNavThumbsSearch: '.jpg',
			controlNavThumbsReplace: '-140x45.jpg',
			keyboardNav:true,
			pauseOnHover:true,
			manualAdvance:false,
			captionOpacity:0,
		});
	});
	</script>
      <div id="slidewrap">
        <div id="slider"> <a title="Join Now" href="?pg=join" class="nivo-imageLink"><img rel="http://cdn.stratuscraft.net/StratusCraf ... /smp/1.png" alt="" src="http://cdn.stratuscraft.net/StratusCraf ... Dragon.jpg" ></a> <a title="Join Now" href="?pg=join" class="nivo-imageLink"><img rel="http://cdn.stratuscraft.net/StratusCraf ... /smp/2.png" alt="" src="http://cdn.stratuscraft.net/StratusCraf ... n.jpg"></a> <a title="Join Now" href="?pg=join" class="nivo-imageLink"><img rel="http://cdn.stratuscraft.net/StratusCraf ... /smp/3.png" alt="" src="http://cdn.stratuscraft.net/StratusCraf ... e.jpg"></a> <a title="Join Now" href="?pg=join" class="nivo-imageLink"><img rel="http://cdn.stratuscraft.net/StratusCraf ... /smp/4.png" alt="" src="http://cdn.stratuscraft.net/StratusCraf ... e.jpg"></a> <a title="Join Now" href="?pg=join" class="nivo-imageLink"><img rel="http://cdn.stratuscraft.net/StratusCraf ... /smp/5.png" alt="" src="http://cdn.stratuscraft.net/StratusCraf ... c.jpg"></a> </div>
      </div>
    </div>
  </div>
</div>
<div class="container">
  <div class="flexslider">
    <ul class="slides">
      <li> <a href="#"><img src="http://cdn.stratuscraft.net/StratusCraf ... /smp/1.png" /></a>
        <p class="flex-caption"><span></span></p>
      </li>
      <li> <a href="#"><img src="http://cdn.stratuscraft.net/StratusCraf ... /smp/2.png" /></a>
        <p class="flex-caption"><span></span></p>
      </li>
      <li> <a href="#"><img src="http://cdn.stratuscraft.net/StratusCraf ... /smp/3.png" /></a>
        <p class="flex-caption"><span></span></p>
      </li>
      <li> <a href="#"><img src="http://cdn.stratuscraft.net/StratusCraf ... /smp/4.png" /></a>
        <p class="flex-caption"><span></span></p>
      </li>
      <li> <a href="#"><img src="http://cdn.stratuscraft.net/StratusCraf ... /smp/5.png" /></a>
        <p class="flex-caption"><span></span></p>
      </li>
    </ul>
  </div>
</div>
<div id="clum2_wrap" class="container">
  <div id="clum2_main">
    <div class="guidewrap">
      <?php
            
            include($include_file);
            
            ?>
      <div class="clear"></div>
    </div>
    <div id="clum2_sidebar_left">
      <div class="clum2_sidebar_unit">
        <li class="widget-container widget_text">
          <h3 class="widget-title">About</h3>
          <div class="textwidget">
            <p> StratusCraft is a dedicated group
              of Minecraft servers featuring large
              builds, amazing staff, custom plugins,
              friendly towns, and a large anti-grief
              system. Your protected!<br />
              <br />
              When you join, you are greeted by any
              currently online players. You will be
              taken to a place with signs stating the
              rules, a few commands, and some other
              guidelines.<br />
              <br />
              Your dreams are endless. Be free, in the
              best server group! </p>
            <p align="right"> <b><a href="?pg=join" />Join Now >></a></b> </p>
          </div>
        </li>
        <li class="widget-container tweet">
          <h3>Stats</h3>
          <center>
            <iframe src="http://isup.stratuscraft.net/stats/isup/"
          width="265"
          height="260"
          scrolling="no"
          allowtransparency="true"
          frameborder="0"> Your browser can't handle awesome-ness, please upgrade. </iframe>
          </center>
        </li>
      </div>
    </div>
  </div>
  <div class="clear"></div>
  <div class="line_main"></div>
  <div id="topwrap">
    <div id="top"></div>
  </div>
  <div id="footer">
    <div id="footbox" class="container">
      <div id="footer_trggle" class="footer_open"></div>
      <div class="footinfo">© Copyright Bleeding Edge Media <?php echo date("Y"); ?></div>
    </div>
  </div>
  <div class="clear"></div>
  <div id="footunder">
    <div id="footunderwrap" class="container">
      <div class="textinfo">
        <p align="right"><img src="http://cdn.stratuscraft.net/StratusCraft/red_strip.png" /></p>
      </div>
      <div class="contactform"> <font color="#999999">
        <p>"StratusCraft" and all subdomains are © copyright <?php echo date("Y"); ?>.<br />
          <br />
          The scripts and code on this website is not to be recreated or copied in any way, this will result in criminal conviction.<br />
          <br />
          By attempting to DDoS this site (or any other server/website) is against the law. All connections are recorded in an off-location server, along with
          the amount of packets sent, and browsers. This information is kept private and will not be sold. If you do not want your information kept, then please
          contact <a href="mailto:admin@stratuscraft.net"><font color="#D7D7D7">the Admin</font></a>. This website is protected against spammers and DDoS by <a href="http://cloudflare.com" target="_BLANK"><font color="#D7D7D7">CloudFlare</font></a></p>
        </font> </div>
    </div>
  </div>
</div>
</body>
</html>
/login/index.php
<?php
	include('../core/DB_connect.php');
	$user=$_POST['user']; 
	$password=$_POST['password'];
	$user = stripslashes($user);
	$password = stripslashes($password);
	$user = mysql_real_escape_string($user);
	$password = mysql_real_escape_string($password);
	$enc=md5($password);
	$sql="SELECT * FROM $tbl_name WHERE username='$user' and password='$enc'";
	$result=mysql_query($sql);
	$count=mysql_num_rows($result);
	if(isset($_POST['user'])){
		if($count==1){
			$_SESSION['user'] = $_POST['user'];
			$_SESSION['passwd'] = $_POST['password'];
			header("Location: ../");
		}else{
			$false_enc = '<b><font color="#CC0000">Incorrect details</font></b>';
			$enc_type = 'text-input-wrong';
		}
	}else{
		$false_enc = 'Need help? Contact the <a href="mailto:abcedea@me.com">Admin</a>';
		$enc_type = 'text-input';
	}
?>

<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">  
        <title>StratusCraft / Login</title>  
        <link rel="stylesheet" href="http://cdn.stratuscraft.net/StratusCraft/login.css">  
        <link href='http://fonts.googleapis.com/css?family= ... 00,700,600' rel='stylesheet' type='text/css'>
        
        <script type="text/javascript" src="http://cdn.stratuscraft.net/jQuery/jque ... "></script>
		<script type="text/javascript" src="http://cdn.stratuscraft.net/jQuery/login.js"></script>
        
    </head>  
    <body bgcolor="#2E2E2E">
        <div class="wrap">  
            <div class="stacked">  
                <h2>StratusCraft</h2>  
                <form action="index.php" method="post" id="login">  
                    <div class="field">  
                        <label for="email">Username</label>  
                        <input type="text" name="user" id="user" class="<?php echo $enc_type; ?>" placeholder="abcedea" />  
                    </div>  
                    <div class="field">  
                        <label for="password">Password</label>  
                        <input type="password" name="password" id="password" class="<?php echo $enc_type; ?>" placeholder="'ooh, secret'" />  
                    </div>
                    <div class="action clearfix"> 
                        <input type="submit" value="Submit" /> 
                        <div class="enc">
                    		<?php echo $false_enc; ?>
                    	</div> 
                    </div>
                </form>
            </div>
            <center>© Copyright <a href="http://stratuscraft.net" class="transition">StratusCraft</a> <?php echo date('Y'); ?></center>
        </div>
    </body>  
</html>
And /login/logout.php
<?php
session_start();

unset($_SESSION['user']);
unset($_SESSION['passwd']);

header('Location: ../');
?>
This really is getting on my nerves now, PHP never used to be this difficult :(
Are you sure I don't have to enable any PHP modules?

Well, if it won't work, then thanks for your help anyway. I may try self-defining cookies or something :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP 5.2 sessions

Post by jacek »

Well the session_start() has to come before any output from the page, but that would normally give you at error. It should really be in your init.inc.php file and nowhere else :)

If that is the problem then you need to make sure you have display_errors set to On as well as the error_reporting setting.
Image
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP 5.2 sessions

Post by Helx »

Ok :D I fixed it!
I'm so happy now, but in case anyone else is having this problem I'll explain how I fixed it.

The variables for the sessions need to be defined in the same if() block. Like this:
if(isset($_POST['user'])){
		if($count==1){
			$duf67f = $_POST['user']; //Set the variable
			
			$_SESSION['log_usr'] = $duf67f; //Define the sessions

			echo "Logged in! : D";
		}else{
			echo "Incorrect credentials.";
		}
Hope it helps. :D
Thanks for your help jacek :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP 5.2 sessions

Post by jacek »

That's odd ! But good that you fixed it :)
Image
Post Reply