session is not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imran.rajani
Forum Newbie
Posts: 16
Joined: Tue Dec 22, 2009 5:34 pm

session is not working

Post by imran.rajani »

friends,

i've just started learning PHP.
I'm stuck on session. i tried my best on internet but writing is now the last option for me.

Login.php

Code: Select all

<html>
<body>
<?php
if(isset($_POST['btnSubmit'])=="Login") {
$user=$_POST['txtUser'];
$password=$_POST['txtPassword'];
$con=mysql_connect("localhost","root","") or die ("error connection");
mysql_select_db("imran",$con) or die ("database not connect");
$qryLogin="SELECT * FROM login where username='$user' AND password='$password'";
$qryExecute=mysql_query($qryLogin);
$qryRow=mysql_fetch_array($qryExecute);
if($qryRow!=NULL){
$_SESSION['uid']=$qryRow['username'];
echo $_SESSION['uid'];
header('Location: products.php');}
 
else{ echo "not login"; }
}
 
?>
 
<form name="loginForm" action="" method="post">
Id: <input type="text" name="txtUser" /><br />
Password: <input type="text" name="txtPassword" /><br />
<input type="submit" name="btnSubmit" value="Login" />
</form>
</body>
</html>
 
i've checked login.php is connecting mysql and works fine.

products.php

Code: Select all

<html>
 
<body>
<?php
session_start();
$uuid=$_SESSION['uid'];
echo $uuid;
?>
 
</body>
</html>
 
i'm actually trying to provide access to product page only if visitor comes from login.php.

i'm getting error "Notice: Undefined index: uid in C:\wamp\www\Practice\Login_and_MySql\products.php on line 6" if i come from login.php.

kindly help me.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: session is not working

Post by Griven »

Add session_start(); to the beginning of your Login.php page.

Every time you want to use sessions on a page, you must first start the session using the above function.
imran.rajani
Forum Newbie
Posts: 16
Joined: Tue Dec 22, 2009 5:34 pm

Re: session is not working

Post by imran.rajani »

session_start not works !!!! :(

is there any configuration issue ?
i mean, need to ON sessions somewhere ???
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: session is not working

Post by AbraCadaver »

imran.rajani wrote:session_start not works !!!! :(

is there any configuration issue ?
i mean, need to ON sessions somewhere ???
You need to add the session_start() before <html> or anything else. Also, you need to turn on error reporting. It will help you see these errors.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
imran.rajani
Forum Newbie
Posts: 16
Joined: Tue Dec 22, 2009 5:34 pm

Re: session is not working

Post by imran.rajani »

It was my mistake.
Session variable was actually not getting value due to stupid If condition.
I fixed and its working now.
AbraCadaver wrote:You need to add the session_start() before <html> or anything else. Also, you need to turn on error reporting. It will help you see these errors.
i learnt somewhere that session_start() is required for calling session variable but not for initializing or setup. I tried and found theory correct.

grateful, if anyone can endorse this concept or guide a bit.
Post Reply