medicalboy Posted November 16, 2011 Share Posted November 16, 2011 i bulit a login system by my own class,with the following code: 1-check.php :-> a class to connect to db,check whether login details present or not,store a session <?php class mysql{private $localhost="localhost";private $user_db="root";private $pass_db="root";private $db_name="flip";function __construct(){ mysql_connect($this->localhost,$this->user_db,$this->pass_db); mysql_select_db($this->db_name); } function sql(){ $username=$_POST['username']; $passowrd=$_POST['password']; $sql="SELECT * FROM users WHERE username='$username' and password='$passowrd' "; $query=mysql_query($sql); $num=mysql_num_rows($query); if($num == 1){ $_SESSION["username"]; $_SESSION["password"]; header("Location:cpanel.php"); } else{ echo"username or password is flase"; } }}$use=new mysql;$use->sql(); 2-cpanel.php: <?phpsession_start();if(!isset($_SESSION["username"])){header("Location:login.php");}?><p>hello</p> i don't know what's the problem code of checking if the a session stored or not: if(!isset($_SESSION["username"])){header("Location:login.php"); it returns me to login again,although the code works wellwhat's the problem with this snippet,and is there an alternative snippet to check a stored session????? Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 if($num == 1){$_SESSION["username"];$_SESSION["password"];} What are you doing there? Right, nothing!You have to set them ;-)And you have to start your session here too! P.S.: Please, please take a look at SQL Injection!!! Link to comment Share on other sites More sharing options...
birbal Posted November 16, 2011 Share Posted November 16, 2011 you need to start session in every page where you need to work with session ..even in check.php Link to comment Share on other sites More sharing options...
Ingolme Posted November 16, 2011 Share Posted November 16, 2011 Like it's said, every single file that uses $_SESSION must have a session_start() call in order to be able to set or read $_SESSION values. Link to comment Share on other sites More sharing options...
medicalboy Posted November 16, 2011 Author Share Posted November 16, 2011 if($num == 1){$_SESSION["username"];$_SESSION["password"];} What are you doing there? Right, nothing!You have to set them ;-)And you have to start your session here too! P.S.: Please, please take a look at SQL Injection!!! thanx,ididn't understand the sessions well, now i get a vlaue and everything is good ----for sql inj,i didn't secure the code,i am just train myself to use oop Link to comment Share on other sites More sharing options...
Ingolme Posted November 16, 2011 Share Posted November 16, 2011 You haven't put any values in the $_SESSION['username'] or $_SESSION['password'] elements. Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 you need to start session in every page where you need to work with session ..even in check.php You haven't put any values in the $_SESSION['username'] or $_SESSION['password'] elements. Why is everyone repeating my answer?I am new here, I know, but I have the feeling that it's common to do this here ... and I'm wondering why ...are you pumping up your posts counter or what is the purpose of this? Link to comment Share on other sites More sharing options...
thescientist Posted November 16, 2011 Share Posted November 16, 2011 they all replied within the same span as you did. it's not uncommon more multiple similar answers to be given with the same few minute span. Also, Ingolme's a moderator, so I think at the very least, he doesn't need to be "showing off" anymore by creating an artificially inflated post count. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2011 Share Posted November 16, 2011 It's common for more people to reply if they have anything additional to add, like a specific function name to use instead of a suggestion to start the session. It's even better if you give links to the manual. Link to comment Share on other sites More sharing options...
medicalboy Posted November 17, 2011 Author Share Posted November 17, 2011 Why is everyone repeating my answer?I am new here, I know, but I have the feeling that it's common to do this here ... and I'm wondering why ...are you pumping up your posts counter or what is the purpose of this? relax my bro,this is agood thing to find everone try to help someone,i hope if i cane help others even i put the samething but with a different method Link to comment Share on other sites More sharing options...
Synook Posted November 17, 2011 Share Posted November 17, 2011 This is not like Stack Overflow; there is no "reputation" you get for giving a "correct" or "best" answer (unless people want to click the "like this" button, which doesn't seem to happen often). Thus, there is no inherent disadvantage to a previous poster if a question is answered in a similar manner multiple times, unless you believe it creates confusion. All contributions are appreciated here, and there is no need to attempt to hoard, or protect your answers. Whatever you have to say, if it is constructive, then it is better said. Link to comment Share on other sites More sharing options...
birbal Posted November 17, 2011 Share Posted November 17, 2011 Why is everyone repeating my answer?I am new here, I know, but I have the feeling that it's common to do this here ... and I'm wondering why ...are you pumping up your posts counter or what is the purpose of this? no one here is to show up anything (i am not sure about you). post count and reputation does not give you anything. do you think thats why pepole around here spent their time? ofcourse not! everyone have many other works to do rather than post pumping here..everyone here want to help others. if post repeats that means either its time beaten (incase of mine. I have too slow net connection here) or they want to add something to help OP not to overrun the previous contributer. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.