Jump to content

[SOLVED] if statement not executing


soycharliente

Recommended Posts

Neither of these bits of code are working for me.

1:

<?php
if ($loggedin) {
echo "<td><a href=\"showThread.php?id=$id\" title=\"$title\">$title</a></td>";
} else {
echo "<td>$title</td>";
}
?>

 

2:

<?php
echo ($loggedin) ? "<td><a href=\"showThread.php?id=$id\" title=\"$title\">$title</a></td>" : "<td>$title</td>";
?>

 

I'm trying to get the link part of the if/ternary to display if you're logged in. I know for a fact that I am logged in because the login link has changed to a logout link.

 

Some code on each page to check for status:

<?php
if (empty($_SESSION["user"])) {
$loggedin = FALSE;
} else {
$loggedin = TRUE;
}
?>

 

And the login script that creates that session variable:

<?php
$loggedIn = FALSE;
if(isset($_POST["submit_login"])) {
$un =     $_POST["un"];
$pw = md5($_POST["pw"]);
dbconnect();
$query = "SELECT * FROM blog_users WHERE username='$un' AND password='$pw'";
$result = mysql_query($query) or DIE("Error: LOGIN. Contact Webmaster.");
if (mysql_num_rows($result) > 0) {
	$r = mysql_fetch_assoc($result);
	$user = $r["username"];
	$pass = $r["password"];
	if ($un == $user && $pw == $pass) {
		$_SESSION["user"] = $un;
		$loggedIn = TRUE;
		$loginError = FALSE;
		header("Location: index.php");
		exit;
	}
	$loginError = TRUE;
} else {
	$loginError = TRUE;
}
dbclose();
}
?>

Link to comment
Share on other sites

Could I make $loggedin a global variable? Would that let it been used everywhere? If so, how do I do that? Register globals is on.

 

You certainly could, but I recommend not doing so.  Avoid globals like the plague.

 

Instead, why not create a class or interface with functions like isLoggedIn(), logout, etc. that check / edit what's in $_SESSION?

 

Then in your site you can just say:

<?php
  if(MySession::isLoggedIn()){
    echo "You are logged in.";
  }else{
    echo "You are not logged in.";
  }
?>

 

Many newer programmers ask what the point of using classes or objects is; well this is one of them.  If you later decide to change how the site tracks logged in users, you only edit your functions MySession::isLoggedIn(), etc. and the rest of the site continues to work.  In other words, if you have to change implementation details later, as long as you keep the interface (in this case, MySession) the same, you don't have to modify code elsewhere.

Link to comment
Share on other sites

Charlie, classes are awesome. I've built a small library of special functions I've written.

 

Instead of copying and pastign code all the time you just upload a class and use it. It's great for hiding code as well and they're extremely versatile.

Link to comment
Share on other sites

The :: operator allows you to access a class method statically, i.e. without an instance of the class.

 

<?php
class Foo{
  function hello(){
    echo "Hello, World!";
  }
}

// Access the function without an instance of the class:
Foo::hello();
?>

 

Keep in mind that the $this variable only exists within instances of a class.

 

(EDIT) In this sense, you can fake name spaces in PHP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.