sandbudd Posted August 14, 2009 Share Posted August 14, 2009 I am just wanting to display the results when one logs in...thought it should be a simple echo statement but not working <?php // check the login details of the user and stop execution if not logged in if(!isset($session['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit; } $row=mysql_fetch_object(mysql_query("select * from signup where userid='$session[userid]'")); echo $row['firstname']; echo $row['lastname']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/ Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 Are you using sessions? If you are $session should be $_SESSION (must be in caps). You should also be calling session_start on any page that uses $_SESSION's Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898483 Share on other sites More sharing options...
p2grace Posted August 14, 2009 Share Posted August 14, 2009 Assuming $session is your actual variable, change your query to this: $row=mysql_fetch_object(mysql_query("select * from signup where userid='{$session[userid]}'")); Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898484 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 changed and same results and it is using sessions just showing the code that did not work Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898487 Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 changed and same results and it is using sessions just showing the code that did not work Have you called session_start() at the very top of your script? eg <?php session_start(); //code for page here ... Also where/how are you defining your session variables? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898488 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 yes I have Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898489 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 do I need to call userid in the echo statement? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898490 Share on other sites More sharing options...
p2grace Posted August 14, 2009 Share Posted August 14, 2009 Before your query, just echo out echo $_SESSION[userid]; and see if anything displays. If it doesn't then you know the session var doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898492 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 <?php session_start(); session_register("session"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898493 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 when I did this echo $_SESSION[userid]; I get nothing Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898494 Share on other sites More sharing options...
dreamwest Posted August 14, 2009 Share Posted August 14, 2009 <?php session_start(); $uid = $_SESSION['userid']; // check the login details of the user and stop execution if not logged in if($uid ==""){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit; } $row=mysql_fetch_object(mysql_query("select * from signup where userid='{$uid}'")); echo $row['firstname']; echo $row['lastname']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898495 Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 You should not be using session_register to create a session variable. Instead you use the $_SESSION superglobal array. Example page1.php <?php session_start(); $_SESSION['name'] = 'John Smith'; ?> <a href="page2.php">Go to Page2.php</a> page2.php <?php session_start(); echo 'Hello! Your name is: ' . $_SESSION['name']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898496 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 now I get the echo warning Sorry, Please login and use this page Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898500 Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 now I get the echo warning Sorry, Please login and use this page Post your full code here for when the user logs in and for the page that requires the user to login. Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898501 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 here is the login page <?php include "include/session.php"; include "include/db.php"; ////////////////////////////// ?> <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>(Type a title for your page here)</title> <meta name="GENERATOR" content="Arachnophilia 4.0"> <meta name="FORMATTER" content="Arachnophilia 4.0"> </head> <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"> <?php $userid=mysql_real_escape_string($userid); $password=mysql_real_escape_string($password); if($rec=mysql_fetch_array(mysql_query("SELECT * FROM signup WHERE userid='$userid' AND password = '$password'"))){ if(($rec['userid']==$userid)&&($rec['password']==$password)){ include "include/newsession.php"; echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>"; print "<script>"; print " self.location='welcome.php';"; // Comment this line if you don't want to redirect print "</script>"; } } else { session_unset(); echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Userid and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898503 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 here is the newsession.php <?php //error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR); $session['id']=session_id(); $session['userid']=$userid; //echo $session['userid']; ?> [/] Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898506 Share on other sites More sharing options...
wildteen88 Posted August 14, 2009 Share Posted August 14, 2009 You're not calling session_start() at the top of your login script. This function needs to be called on the first line of any page that uses sessions (this does not include files that are being included). It cannot be called after any output has been made (eg, text, html etc). So add session_start() to your login script as highlighted below: <?php session_start(); include "include/session.php"; include "include/db.php"; ////////////////////////////// ?> Again $session needs to be $_SESSION within newsession.php Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898509 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 when I change to this I get the user id echo $session['userid']; Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898512 Share on other sites More sharing options...
p2grace Posted August 14, 2009 Share Posted August 14, 2009 Then this should work $row=mysql_fetch_object(mysql_query("select * from signup where userid='{$session['userid]'}'")); And I'm not even going to attempt to figure out how your $session varible is populated since it clearly isn't from the $_SESSION. Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898515 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 when I put that in I get a black page? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898520 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 I dont understand because when I use this echo $session['userid']; I do get the userid? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898525 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 do I need to include the other fields in the statement? $row=mysql_fetch_object(mysql_query("select * from signup where userid='{$session[userid]}'")); Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898534 Share on other sites More sharing options...
sandbudd Posted August 14, 2009 Author Share Posted August 14, 2009 any ideas guys? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898538 Share on other sites More sharing options...
sandbudd Posted August 15, 2009 Author Share Posted August 15, 2009 did everyone bale out? Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898548 Share on other sites More sharing options...
p2grace Posted August 15, 2009 Share Posted August 15, 2009 Sorry I had a syntax issue, try this: $row=mysql_fetch_object(mysql_query("select * from signup where userid='{$session['userid']}'")); Quote Link to comment https://forums.phpfreaks.com/topic/170323-solved-simple-echo-statement-not-working/#findComment-898553 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.