Jump to content

[SOLVED] $_SESSION['uid'] won't stay open


dachshund

Recommended Posts

for some reason as soon as i log (which works fine) the session closes and it says i am no longer logged in.

 

here's my log in form

 


<?php

		if(isset($_SESSION['uid'])){
			$sessionname = $_SESSION['uid'];
			$accountsql = "SELECT * FROM users WHERE `userid` = '$sessionname'";
			$accountres = mysql_query($accountsql) or die(mysql_error());
			$accountrow = mysql_fetch_assoc($accountres);

			echo 'Hello ';
			echo $accountrow['username'];
			echo ' ( ';
			echo $accountrow['email'];
			echo ' ).';
			echo '<div id="profilepicture">';
			echo '<img src="/images/user/';
			echo $accountrow['userid'];
			echo '/profilepicture.jpg" width="60" height="60">';
			echo '</div>';
			echo 'Upload a new icon';

		}else	{
			if(!$_POST['login']){
				echo '<form name="login" method="post" action="#">';
				echo '<p>Username: <input type="text" name="username"></p>';
				echo '<p>Password: <input type="password" name="password"></p>';
				echo '<p><input type="submit" name="login" value="Submit"></p>';
				echo '</form>';
			}else	{
				$username = $_POST['username'];
				$password = $_POST['password'];

				if($username && $password){
					$sql = "SELECT * FROM users WHERE `username`='$username'";
					$res = mysql_query($sql) or die(mysql_error());

						if(mysql_num_rows($res) == 1){
							$epass = md5($password);
							$sql2 = "SELECT * FROM users WHERE `username` = '$username' AND `password` = '$epass'";
							$res2 = mysql_query($sql2) or die(mysql_error());

							if(mysql_num_rows($res2) ==1){
								//success

								$rows = mysql_fetch_assoc($res2);
								$_SESSION['uid'] = $rows['userid'];
								echo 'You are logged in as ';
								echo $username;
							}else	{
								echo 'Wrong username and/or password';
							}
						}else	{
							echo 'Username does not exist.';
						}
				}else	{
					echo 'Please fill in all fields.';
				}
			}
		}
		?>

 

i have session_start() in the header of every page etc etc. this used to be working fine but now it doesn't work at all and I don't know what's changed.

 

when i put this in the header of the log in page

 


ini_set("display_errors", "1");
error_reporting(E_ALL);

 

it says this before i log in

 

NOTICE: UNDEFINED INDEX: UID

 

any help would be great

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/
Share on other sites

Here's the log in form in full and the header below that.

 


<div id="site_container">

<?

include "../template/header.php";

?>

<div id="content">

        <div id="viewlogin">
        
        	<div id="viewtitle">
            <?php

		if(isset($_SESSION['uid'])){
            echo 'Account';
		}else	{
		echo 'Log In';
		}
		?>

            
            </div>
            
            <div id="viewcontent">
            
            <?php

		if(isset($_SESSION['uid'])){
			$sessionname = $_SESSION['uid'];
			$accountsql = "SELECT * FROM users WHERE `userid` = '$sessionname'";
			$accountres = mysql_query($accountsql) or die(mysql_error());
			$accountrow = mysql_fetch_assoc($accountres);

			echo 'Hello ';
			echo $accountrow['username'];
			echo ' ( ';
			echo $accountrow['email'];
			echo ' ).';
			echo '<div id="profilepicture">';
			echo '<img src="/images/user/';
			echo $accountrow['userid'];
			echo '/profilepicture.jpg" width="60" height="60">';
			echo '</div>';
			echo 'Upload a new icon';

		}else	{
			if(!$_POST['login']){
				echo '<form name="login" method="post" action="#">';
				echo '<p>Username: <input type="text" name="username"></p>';
				echo '<p>Password: <input type="password" name="password"></p>';
				echo '<p><input type="submit" name="login" value="Submit"></p>';
				echo '</form>';
			}else	{
				$username = $_POST['username'];
				$password = $_POST['password'];

				if($username && $password){
					$sql = "SELECT * FROM users WHERE `username`='$username'";
					$res = mysql_query($sql) or die(mysql_error());

						if(mysql_num_rows($res) == 1){
							$epass = md5($password);
							$sql2 = "SELECT * FROM users WHERE `username` = '$username' AND `password` = '$epass'";
							$res2 = mysql_query($sql2) or die(mysql_error());

							if(mysql_num_rows($res2) == 1){
								//success

								$rows = mysql_fetch_assoc($res2);
								$_SESSION['uid'] = $rows['userid'];
								echo 'You are logged in as ';
								echo $username;
							}else	{
								echo 'Wrong username and/or password';
							}
						}else	{
							echo 'Username does not exist.';
						}
				}else	{
					echo 'Please fill in all fields.';
				}
			}
		}
		?>

            
            </div>
        
        </div>
    
    	<div id="rightcolumn">
    
    	<?php

	include "../mostpopular.php";

	?>
    
    	<div id="spacer"></div>
    
    	<?php

	include "../news.php";

	?>
    
    	</div>

<div class="clear"></div>
    
    
    </div>

<?

include "../template/footer.php";

?>

</div>

 


<?php
   
   $connect= mysql_connect ("-", "-", "-") or die(mysql_error());
   $db = mysql_select_db ("-", $connect);
   
   session_start();
  
?>



<link href="/template/stylesheet.css" rel="stylesheet" type="text/css" />

    <!-- TOOLBAR -->
    
    
    <div id="toolbar">
    
   		<div id="follow">
        	<ul>
            	<li>
                <img src="h/images/follow/twitter_icon.png" />
                </li>
                <li>
                <img src="/images/follow/facebook_icon.png" />
                </li>
                <li>
                <img src="/images/follow/myspace_icon.png" />
                </li>
                <li>
                <img src="/images/follow/rss_icon.png" />
                </li>
            </ul>
        </div>
        
        <div id="usertools">
        	<div id="login">
            	<ul>
				<li class="login_text">
                    <?php

				if(isset($_SESSION['uid'])){

					echo '<li class="login_text"><a href="';
					include $_SERVER['DOCUMENT_ROOT'];
					echo '/test2/profile/">Account</a></li>';
					echo '<li class="login_text"><a href="';
					include $_SERVER['DOCUMENT_ROOT'];
					echo '/test2/profile/logout.php">Logout</a></li>';

				}else	{
					echo '<li class="login_text"><a href="';
					include $_SERVER['DOCUMENT_ROOT'];
					echo '/test2/profile/">Log In</a></li>';
					echo '<li class="login_text"><a href="';
					include $_SERVER['DOCUMENT_ROOT'];
					echo '/test2/profile/register.php">Register</a></li>';
				}

				?>

                </ul>
            </div>
                <form id="search_form">
                	<input type="text" id="search_form" class="login_text" value="Search">
                </form>
            
            <div class="clear"></div>
        </div>
        
        <div class="clear"></div>
    
    </div>
    
    
    
    
    <!-- HEADER -->
    
    
    <div id="header">

HUH. Magazine

</div>
    
    
    <!-- NAVIGATION -->
    
    <div id="navigation">
   	
    	<ul>
        
        	<li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/index.php">
            Home
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/feature/">
            Features
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/interview/">
            Interviews
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/review/">
            Reviews
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/blog/">
            Blog
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/shop/">
            Shop
            </a>
            </li>
            
            <li>
            <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/tv/">
            TV
            </a>
            </li>
        
        </ul>
    
    </div>

I'm just learning about HTTP headers... so this is just a stab in the dark: in your header file, delete everything before the first occurrence of "<?php".

 

Blank lines, spaces, etc.

The file should start out with "<?php" at the very top of the file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.