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(); } ?> Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/ 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291274 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291276 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) Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291285 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!!! Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291289 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291292 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! Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291297 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291299 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291301 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. Link to comment https://forums.phpfreaks.com/topic/58723-solved-if-statement-not-executing/#findComment-291366 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.