cool_techie Posted January 29, 2011 Share Posted January 29, 2011 I have written my index.php script where after verifying the password the script displays the user's name. But the user name is not displayed by it. Here is my script [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/ Share on other sites More sharing options...
doddsey_65 Posted January 29, 2011 Share Posted January 29, 2011 where are you getting $_REQUEST[uname] from? is it a form on a different page? also this line is wrong: $select=mysql_query("SELECT * FROM user_id WHERE uname='$_REQUEST[uname]'",$connection); it should be: $select=mysql_query("SELECT * FROM user_id WHERE uname='".$_REQUEST['uname']."'"); is user_id the name of the database table? Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166892 Share on other sites More sharing options...
harristweed Posted January 29, 2011 Share Posted January 29, 2011 Are you sure that: 1. $_REQUEST['password'] is set? 2. Your SELECT Query is working correctly. Have you really got a table called 'user_id' ? Try echoing out the variables and see what you get. Turn error reporting on. Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166894 Share on other sites More sharing options...
doddsey_65 Posted January 29, 2011 Share Posted January 29, 2011 for a better chance of seeing the error do this. replace the top php code with: $connection=mysql_connect("localhost","root",""); mysql_select_db("forum",$connection); $select=mysql_query("SELECT * FROM user_id WHERE uname='".$_REQUEST[uname]."'") or die(mysql_error()); $row=mysql_fetch_array($select); if($row) { if($row['password']==$_REQUEST['password']) { session_start(); $_SESSION['name']=$_REQUEST['uname']; } else { header("Location:err-login.php"); } } else { die('Query Failed'); } i have added the mysql_error() function which will show any errors within your query. I have also fixed your query. in the bit where you call $_SESSION['name'] replace it with: if(isset($_SESSION['name'])) { echo "Welcome ".$_SESSION['name']; } else { echo 'No Username Set Into Session[\'name\']'; } this will say if there is no session set. I must say though your code is a mess. split the css, html and php into different files. also on html attributes like: <p align=center> use quotes on the values: <p align="center"> or for better markup use proper css styling: <p style="text-align:center;"> Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166897 Share on other sites More sharing options...
cool_techie Posted January 29, 2011 Author Share Posted January 29, 2011 Somehow I was able to manage the problem. What i did was that my earlier script was: <h2><?php { if(isset($_SESSION['name'])) echo "Welcome ".$_SESSION['name']; } ?></h2> I only removed that h2 element and inserted in echo like this: if(isset($_SESSION['name'])) echo "<h2>Welcome ".$_SESSION['name'],"</h2>"; And now it is working fine..! Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166898 Share on other sites More sharing options...
cool_techie Posted January 29, 2011 Author Share Posted January 29, 2011 @doddsey_65 Sorry for the messy code.... It was because my friend handles all the html+css and I handle the php..So i did not try to make any efforts there...! I have some problems here: like what is the difference b/w $row['name'] and '$row[name]' ...?? I always had trouble in this quoting stuff.. Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166900 Share on other sites More sharing options...
doddsey_65 Posted January 29, 2011 Share Posted January 29, 2011 $row[name] means nothing, it would throw up the error use of undefined constand because name hasnt been defined. however $row['name'] is the right way to use it. consider: $array = array('one', 'two'); echo $array[one]; echo $array['two'] the first echo would cause an error stopping the script entirely(depending on your error handling) but he second echo is correct. Make sense? also '$row[name]' would just print $row[name] and not the actual array value since anything in single quotes isnt parsed by php. Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166904 Share on other sites More sharing options...
cool_techie Posted January 29, 2011 Author Share Posted January 29, 2011 Yeah I got it...but still when we modify scripts we use it: Consider this code $x="INSERT INTO user_id (name,uname,password,email,contact) VALUES ('$_POST[name]','$_POST[uname]','$_POST[password]','$_POST','$_POST[contact]')"; It is working fine..... whereas when i changed it to $_POST['name'] it was giving error. Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166905 Share on other sites More sharing options...
doddsey_65 Posted January 29, 2011 Share Posted January 29, 2011 well if its working thats news to me but it is bad coding practice. it should be: $x="INSERT INTO user_id (name,uname,password,email,contact) VALUES ('".$_POST['name']."','".$_POST['uname']."','".$_POST['password']."','".$_POST['email']."','".$_POST['contact']."')"; Quote Link to comment https://forums.phpfreaks.com/topic/226028-unable-to-show-session-name/#findComment-1166909 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.