soycharliente Posted July 6, 2007 Share Posted July 6, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 Instead of checking the variable $loggedin check a session variable instead as the contents of $loggedin will be lost when you access a new script. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 6, 2007 Author Share Posted July 6, 2007 Bang-a-rang. Thanks. 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. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 6, 2007 Author Share Posted July 6, 2007 Now the links still render when I logout. <?php if (isset($_GET["logout"])) { session_destroy(); $loggedin = FALSE; } ?> The URL: http://www.meh.com/xxx/xxx/index.php?logout (not real) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 Globals = very scary. Best keep them local and declare them global only when needed!!! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 6, 2007 Share Posted July 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 roopurt, do you know of any links I can read up on the :: operator? I've never seen that. My classes use -> instead! Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 6, 2007 Author Share Posted July 6, 2007 I have no idea how to write classes, so that's a new thing to me. Until then, I just wrote separate functions with different lines at the line in question. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 6, 2007 Share Posted July 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.