Stalingrad Posted July 29, 2008 Share Posted July 29, 2008 Hi! I;m having major diffuculties with PHP Sessions. I created a Login and everything, and it seems to work fine, but no matter what I change, I can't get it to display the user their username when they logged in. Also; since I have interaction on my site, the activities where you require a username fill in blank for the username. I have no clue what I am doing wrong, but this did work fine yesterday, and ever since I logged out, it's not displaying the information. May somebody please give me an answer as to what I'm doing wrong? Thank You so much! If it helps any; I've included several files. Login.php: <?php include("config.php"); if(session_is_registered("username")) { echo "<div id=\"b\">$b</div><div id=\"n\">$nav</div><div id=\"c\"><img src=images/layout/login.jpg><br><br>Error! You are already Logged In.</div><div id=\"i\">$info</div>"; } $submit = $_POST['submit']; $lusername = $_POST['username']; $lpassword = $_POST['password']; if(!session_is_registered("username")) { echo "<div id=\"b\">$b</div><div id=\"n\">$nav1</div></div><div id=\"i\">$info</div><div id=\"c\"><img src=images/layout/login.jpg><br><br>"; if(!isset($submit)) { ?><html><form action="<?php echo "$PHP_SELF" ?>" method="POST">Username: <input type="text" name="username" maxlength="16"><br><br>Password: <input type="password" name="password" maxlength="16"><br><br><br><input type="submit" name="submit" value="Login"></form></html><?php } if(isset($submit)) { $cq = mysql_query("SELECT username FROM users WHERE username='$lusername' AND password='$lpassword'"); $c = @mysql_num_rows($cq); if($c == "0") { ?> <html><font color=red>Error! Invalid Username/Password Combination.</font><br><br><form action="<?php echo "$PHP_SELF" ?>" method="POST">Username: <input type="text" name="username" maxlength="16"><br><br>Password: <input type="password" name="password" maxlength="16"><br><br><br><input type="submit" name="submit" value="Login"></form></html><?php } if($c == "1") { session_register("username"); session_register("password"); echo "You are now Logged in.<br><br>Click <a href=index.php>Here</a> to Continue."; } } } ?> config.php: <?php session_start(); include("style.css"); mysql_connect("mysql4.freehostia.com", "sonben47_db", ""); mysql_select_db("sonben47_db"); $nav = "<img src=images/layout/t.jpg><br><a href=index.php>Home</a><br><img src=images/layout/d.jpg><br><a href=news.php>News</a><br><img src=images/layout/d.jpg><br><a href=map.php>Explore</a><br><img src=images/layout/d.jpg><br><a href=newrock.php>Get a New Rock</a><br><img src=images/layout/d.jpg><br><a href=bag.php>Inventory</a><br><img src=images/layout/d.jpg><br><a href=myrocks.php>My Rocks</a><br><img src=images/layout/d.jpg><br><a href=games.php>Games</a><br><img src=images/layout/d.jpg><br><a href=logout.php>Logout</a><br><br>"; $nav1 = "<img src=images/layout/t.jpg><br><a href=index.php>Home</a><br><img src=images/layout/d.jpg><br><a href=login.php>Login</a><br><img src=images/layout/d.jpg><br><a href=register.php>Register</a><br><br>"; $b = "<img src=images/layout/b.jpg>"; $username = $_SESSION['username']; $uq = mysql_query("SELECT * FROM users WHERE username='$username'"); while($u = mysql_fetch_array($uq)) { $sun = $u['username']; $sar = $u['rock']; $srp = $u['points']; $sid = $u['userid']; } $info = "$_SESSION[username]<br>$sar<br>RP: $srp<br>"; ?> page.php (this is a page that will display the information, but it won't work): <?php session_start(); include("config.php"); if(session_is_registered("username")) { echo "<div id=\"b\">$b</div><div id=\"n\">$nav</div><div id=\"i\">$info</div><div id=\"c\"><font size=6>Page</font><br><br></div>"; } if(!session_is_registered("username")) { echo "<div id=\"b\">$b</div><div id=\"n\">$nav1</div><div id=\"i\"></div><div id=\"c\">Error! Please <a href=login.php>Login</a> or <a href=register.php>Register<a> to Continue.</div>"; } ?> Any help is appreciated, thank you! Link to comment https://forums.phpfreaks.com/topic/117208-session-help-please/ Share on other sites More sharing options...
d.shankar Posted July 29, 2008 Share Posted July 29, 2008 Remove the session_start() from your page.php Link to comment https://forums.phpfreaks.com/topic/117208-session-help-please/#findComment-602904 Share on other sites More sharing options...
Stalingrad Posted July 29, 2008 Author Share Posted July 29, 2008 Thank You, but it still doesn't work. It isn't showing the username. Link to comment https://forums.phpfreaks.com/topic/117208-session-help-please/#findComment-602907 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2008 Share Posted July 29, 2008 session_register() and session_is_registered() were depreciated long ago in php4.2 in the year 2002. Those two functions only work when register_globals are on and those two functions, along with register_globals, have been completely removed in php6. You also have this other thread on this forum which is using sessions the proper way - http://www.phpfreaks.com/forums/index.php/topic,209117.0.html Your mysql_query() statement(s) have no error checking, error reporting, or error recovery logic to get your code to tell you if or why they failed and you have also put an @ in front of at least one mysql function call, suggesting that you are getting errors. Link to comment https://forums.phpfreaks.com/topic/117208-session-help-please/#findComment-602916 Share on other sites More sharing options...
d.shankar Posted July 29, 2008 Share Posted July 29, 2008 So try to use this code below instead of session_is_registered thing as PFMaBiSmAd says it is deprecated... if(!isset($_SESSION['username']) || empty($_SESSION['username'])) { echo "Not Authenticated"; } else { echo "Authenticated"; } Replace all ur code with the above and check it out. Link to comment https://forums.phpfreaks.com/topic/117208-session-help-please/#findComment-602926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.