<?php
session_start();

$con = mysql_connect("localhost","root","");
$sql = mysql_select_db("blog", $con) or die(mysql_error());

?>

<html>
	<head>
		<title>///</title>
	</head>
		<body>
			<div id="admin-content">
			<?php
			
			if($_SESSION['uid']){
				$sel = "SELECT * FROM `users` WHERE `id`='".$_SESSION['uid']."' ";
				$result = mysql_query($sql) or die (mysql_error());
		
			if(mysql_num_rows($res) == 0){
				session_destroy();
			echo "Please <a href='login.php'>Login</a> to your account, or <a href='register.php'>Register</a> new account!";
				}else{
				$row = mysql_fetch_assoc($res);
				echo "Welcome back, <a href='index.php?act=profile&id=".$row['id']."'>".$row['username']."</a>! <a href='logout.php' onClick='return confirmLogout()'>logout</a>";
				if($row['admin'] == '1'){
								echo "admin Section";
							}
				
					}
					} else {
					echo "Please <a href='login.php'>Login</a> to your account, or <a href='register.php'>Register</a> new account!";
					}
					
			?>
		</div>
		</body>
</html>

I got error on line 17, which is "Undefined Index: uid". I'm a new in php please help me guys

Recommended Answers

All 4 Replies

Try replacing line 17 with the following if statement:

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

thanks sir it works...but why? can you explain sir?

thanks sir it works...but why? can you explain sir?

Well basically when checking for the value of a variable (in this case checking in the if statement) you first must check if it's defined unless you know it's defined. So in this instance the variable $_SESSION was never given a value so it triggered an error. However when the if (isset($_SESSION) was put before it, php check if the variable had a value then php found out that no it didn't so false the if statment and didn't pass on any error messages.

thanks sir for helping me. I must defined 1st my variable before the declaration of $_SESSION...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.