Jump to content

What's The Problem With This Code,it Can't Start Session


medicalboy

Recommended Posts

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

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...