Zoroaster Posted September 3, 2009 Share Posted September 3, 2009 I've viewed so many different tutorials etc. and made so many codes that I actually now don't understand some of my code. Please do take a look: <?php session_start(); if(!session_is_registered(myusername)) { header("location:index.php"); } The thing that confuses me is "myusername" Where the hell is it coming from? I can't remember why I put it there when I first did. And now it's confusing me to pieces! It's nothing in the database, it's not a variable, and it's not the name of some html stuff either. So what is it?? Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/ Share on other sites More sharing options...
trq Posted September 3, 2009 Share Posted September 3, 2009 That piece of code is redundant anyway, session_is_registered is deprecated. Oh, but for it to have been valid (when it was still in use), session_is_registered expects a string. if(!session_is_registered('myusername')) { Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911537 Share on other sites More sharing options...
Zoroaster Posted September 3, 2009 Author Share Posted September 3, 2009 That piece of code is redundant anyway, session_is_registred is deprecated. I'm not a very good php programmer, so can you tell me what that means? Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911539 Share on other sites More sharing options...
trq Posted September 3, 2009 Share Posted September 3, 2009 That piece of code is redundant anyway, session_is_registred is deprecated. I'm not a very good php programmer, so can you tell me what that means? It means its no longer in use. Its old code. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911541 Share on other sites More sharing options...
ignace Posted September 3, 2009 Share Posted September 3, 2009 myusername is nothing probably some code from some tutorial where it made sense. And like thorpe said it's deprecated use: session_start(); if (!isset($_SESSION['myusername'])) { $_SESSION['myusername'] = $username; } Edit: deprecated means it is no longer supported and will be removed in a future version. Wikipedia puts it in this way: the term deprecation is applied to software features that are superseded and should be avoided. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911542 Share on other sites More sharing options...
Zoroaster Posted September 3, 2009 Author Share Posted September 3, 2009 So is "myusername" something I have specified?if so where? Cos I've tried editing it to 'username' but that doesn't work... For some reason it has to be "myusername" Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911543 Share on other sites More sharing options...
Kryllster Posted September 3, 2009 Share Posted September 3, 2009 Here is what I use for my login script hope it helps $username comes from the database <?php session_start(); $_SESSION['username'] = $username; ?> mysuername just probably comes from a tutorial or something where you may have copied and pasted just find out what they are supposed to log in under or what have you or define myusernam. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911545 Share on other sites More sharing options...
waynew Posted September 3, 2009 Share Posted September 3, 2009 Firstly; a quote from PHP.net This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. It should be done like so: session_start(); if(!isset($_SESSION['myusername'])) { header("location:index.php"); } Check the part of your script where you log a user in... If you're not setting it there, then you've made a typo or something. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911546 Share on other sites More sharing options...
trq Posted September 3, 2009 Share Posted September 3, 2009 So is "myusername" something I have specified?if so where? Cos I've tried editing it to 'username' but that doesn't work... For some reason it has to be "myusername" Yes, and how should we know. It means that you have likely registered an index of 'myusername' within the $_SERSSION array somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911547 Share on other sites More sharing options...
Zoroaster Posted September 3, 2009 Author Share Posted September 3, 2009 So even though "myusername" was specified in a DIFFERENT php file it still finds it? I certainly havent specified "myusername" in the php file i took the code out from. It's just the code I paste in to make users not be able to go to the pages if they are not logged in. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911549 Share on other sites More sharing options...
waynew Posted September 3, 2009 Share Posted September 3, 2009 SESSION variables can be accessed from page to page as long as you have session_start(); at the top of each page. So that myusername variable could have been (and probably was) set on another page. Most likely in your login script. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911552 Share on other sites More sharing options...
trq Posted September 3, 2009 Share Posted September 3, 2009 You need to take a look at what sessions are. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911554 Share on other sites More sharing options...
Zoroaster Posted September 3, 2009 Author Share Posted September 3, 2009 Ok, I checked my checklogin.php file and I found this: $count = mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } Is that where I specified it? Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911555 Share on other sites More sharing options...
waynew Posted September 3, 2009 Share Posted September 3, 2009 Yes: But change it to this: $count = mysql_num_rows($result); if($count==1){ $_SESSION['myusername'] = true; $_SESSION['mypassword'] = true; header("location:login_success.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911557 Share on other sites More sharing options...
trq Posted September 3, 2009 Share Posted September 3, 2009 Yes. However, session_register is also deprecated. I'm not sure when your wrote this code or where your learning php from, but its out of date. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911558 Share on other sites More sharing options...
waynew Posted September 3, 2009 Share Posted September 3, 2009 Also; please note that you're not actually giving the session variable "myusername" any value. You're just setting it as a variable. To give it the actual username of the user, you'll have to take the username out of the result set you got from MySQL and hand the username over to $_SESSION['myusername'] Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-911560 Share on other sites More sharing options...
Zoroaster Posted September 4, 2009 Author Share Posted September 4, 2009 <? $host = "localhost"; $username = "zoroaste"; $password = "****"; $db_name = "zoroaste_test1"; $tbl_name = "Login"; mysql_connect($host, $username, $password) or die("Failed to connect to mysql!"); mysql_select_db($db_name) or die ("Failed to connect to mysql!"); $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Alright this is the whole checklogin.php file. I'm still confused. Where am I specifying myusername and what am i specifying it to? I know that it's deprecated, but lets just look away from that. I'm trying to make a list of all the registered members post itself on the site. And this is confusing me. I can only manage to make it show the user you're logged in to. Quote Link to comment https://forums.phpfreaks.com/topic/172954-difference-between-myusername-and-username/#findComment-912507 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.