eRott Posted February 22, 2007 Share Posted February 22, 2007 Ok, what I am trying to do is if the user is not logged in, then display a message saying to login. But, if they are logged in, then display a welcome message. Here's the code. What am I doing wrong? HEADER.PHP ------------ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>TITLE</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="en-us" /> <meta name="keywords" content="css template" /> <link href="../../lib/style.css" rel="stylesheet" type="text/css"> <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="rss/" /> </head> <body> <div id="page-container"> <div id="header"><img src="../../images/logo.png" border="0"></div> <div id="menu"> <div class="padding"> <?php include("../../lib/nav.php"); ?> </div> <div class="search"> <?php include("admincp.php"); ?> </div> </div> <div id="content"> <table width="100%" align="center" border="0" cellpadding="5"> <tr> <td rowspan="6" valign="top" width="25%"> <div class="bar"><h3>Navigation</h3></div> <div class="nav2"> <?php include("nav.php"); ?> </div> <br><br> <div class="bar"><h3>Videos</h3> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus dolor nibh, sollicitudin vitae, eleifend quis, pellentesque id, justo. Nullam dui. Sed porta leo a dui. Ut ante. Proin dignissim, pede vitae tristique accumsan, est diam feugiat ante, sed rhoncus sapien lacus eu lacus. Suspendisse eu sem. </div> </td> <td valign="top" width="50%"> <div class="bar"> EXAMPLE PAGE WHERE I WANT THE MESSAGE TO BE DISPLAYED ------------------------------------------------------------- <? session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> <?php include("lib/header.php"); ?> <h3>List Articles</h3> <?php //connect to and select db include 'lib/config.php'; include 'lib/db_open.php'; //get a list of the info from the table and list all articles $sql = "SELECT * from $tbl_content"; $result = mysql_query($sql, $conn) or die(mysql_error()); // for each row fetched from the results... while ($list = mysql_fetch_array($result)) { //display in format echo "<font size='1'>({$list['id']})</font><b>{$list['title']} - <i>{$list['date']}</i></b>: {$list['content']}<br><br>"; } // end while include 'lib/db_close.php'; ?> <?php include("lib/footer.php"); ?> ADMINCP.PHP ------------- <? if(!session_is_registered(myusername)){ echo "<font color="red">Please log in using the form below</font>"; } else { echo "Welcome {$_SESSION['myusername']}"; } ?> [i didn't include the footer because its not important] Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/ Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 1. Use <?php instead of <? 2. Show the code where you registered your session 3. Try to avoid interpolation on an array. For example instaed of <?php if(!session_is_registered(myusername)){ echo "<font color="red">Please log in using the form below</font>"; } else { echo "Welcome {$_SESSION['myusername']}"; } ?> do <?php if(!session_is_registered(myusername)){ echo "<font color="red">Please log in using the form below</font>"; } else { echo "Welcome " . $_SESSION['myusername']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191458 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 The reason I ask where you create it if you are using session_register well don't the php.net manual says it's deprecated. Instead use $_SESSION['username'] = $_POST['username']; or whatever else the variable is. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191459 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 Also make sure session_start is at the top of the page where the session is registered, as well as any other pages the session is going to travel through. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191460 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 Well, Im pretty new to sessions. So I am not too sure what you mean by that. But here is where I register the session: <?php include 'lib/config.php'; include 'lib/db_open.php'; // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_login WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to users location session_register("myusername"); session_register("mypassword"); header("location:index2.php"); } else { echo "Wrong Username or Password"; } ?> Also make sure session_start is at the top of the page where the session is registered, as well as any other pages the session is going to travel through. It is. Except for where the session is registered. Why does it have to be there? Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191462 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 You are registering a name, and not a session. You are telling it to create a session with a blank value, and either way you don't want session_register do session_register("myusername"); session_register("mypassword"); $_SESSION['myusername'] = "value"; You DO-NOT want to register a session with the password, you don't want to and have no need to it's a security risk. I advice just registering the username, as shown above. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191464 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 Actually to make it easier for you, replace both those session register functions with this $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['logged'] = "yes"; This will set the username, then you can use logged to test if they are logged in or not after authorization. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191471 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 So, do this: <?php include 'lib/config.php'; include 'lib/db_open.php'; // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM $tbl_login WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to users location $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['logged'] = "yes"; header("location:index2.php"); } else { echo "Wrong Username or Password"; } ?> But by doing that, will that cause this not to work:? <? session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191475 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 Change that to <?php session_start(); if(isset($_SESSION['myusername'])) { header("location:index.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191479 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 But isnt that saying: If the user is logged in go to index.php? Its supposed to redirect you to index.php if you are not logged in. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191481 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 <?php session_start(); if(!isset($_SESSION['myusername'])) { header("location:index.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191488 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 Ok, so, if i were to write only: <?php echo "Welcome " . $_SESSION['myusername']; ?> then it works fine. however, when I add the other part to it: <?php session_start(); if(!isset($_SESSION['myusername'])) { echo "Welcome " . $_SESSION['myusername']; } else { echo "<font color="red">Please log in using the form below</font>"; } ?> then it doesnt work. The page does not load after it hits that point. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191497 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 I apologize, my fault. Try this <?php session_start(); if(!isset($_SESSION['myusername'])) { echo "<font color="red">Please log in using the form below</font>"; } else { echo "Welcome " . $_SESSION['myusername']; } ?> There you go, that should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191501 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 Nope. The page still stops loading. Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191504 Share on other sites More sharing options...
eRott Posted February 22, 2007 Author Share Posted February 22, 2007 Ok, I have figured out the problem, but don't know how to fix it. If you look at the code below you will notice that at the very beginning I declare that if the user is not logged in, then redirect to index.php. But then, in the header file, I also tell it that if the user is not logged in, then to display some text. So, I am curious, how would I display certain text if the user is logged in, then display some other text if the user is not logged in, without using the same code twice (which is causing the problem). <?php session_start(); if(!isset($_SESSION['myusername'])) { header("location:index.php"); } ?> <?php include("lib/header.php"); ?> <h3>Guild News Editor - New Article</h3> <? if(isset($_POST['add'])) { include 'lib/config.php'; include 'lib/db_open.php'; $title = $_POST['title']; $content = $_POST['content']; $query = "INSERT INTO $tbl_content (title, content, date, submitted) VALUES ('$title', '$content', CURRENT_DATE, '{$_SESSION['myusername']}')"; mysql_query($query) or die('Error, insert query failed'); include 'lib/db_close.php'; echo "<meta http-equiv='refresh' content='1;url=index2.php'>New article successfully added!"; } else { echo " <form method='post'> <b>Title:</b><br><input name='title' type='text' id='title'><br><br> <b>Content:</b><br> <textarea name='content' id='content' type='text' rows='15' cols='50' style='width: 100%; margin-left: 0px; padding: 0px; background-color: #fff'></textarea><br><br> <input name='add' type='submit' id='add' value='Submit New Article'> </form> "; } ?> <?php include("lib/footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191512 Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 <?php if (isset($_SESSION['whatever'])) { echo "Logged in"; }else { echo "Not logged in"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39660-solved-php-session-problem/#findComment-191536 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.