PHP - Login Help: Staying Logged In
Hi. I'm new to php. I created a register, log in, welcome, and log out page for my site. They are all working accordingly. I'm having one problem that I can't seem to fix. I would like to stay logged in as I navigate other pages of my site. Everytime i click on a link to another page within my site I get logged out. I know this issue might pertain to using session but thats all I was able to find using google search. Can anyone help? Much appreciated.
Similar TutorialsHi guys, Im building a website for a company http://www.eminence-logistics.co.uk and the client wants a log in system which im having problems with. The registration system is all working fine. If you go to the website you can see where the login form is. Now when the user isn't logged in i need the form to show, but when the user IS logged in i would like a message replacing the form saying welcome, along with a log out button. How do i do this? Thankyou very much Enlighten Hi, i have a site www.gnetuk.net where people can register and login,. thats all fine but what code can i use on my php pages to see whos logged in im sure its going to be a simple line of code or smthink but i am new to this stuff. PLEASE HELP <G> Hi all, I have a problem I am not sure how to sort, hopefully someone here can help. I have a sliding login panel on a website that I am making. Its quite discreet. I have a stripped down version here that you can see: http://mgdesign.hostultra.com/login_test/login_test.php On the page I am making, users with a log in enter their details and will be taken to the restricted page. I would like this sliding panel to be on every page when the user is not logged in. Once they log in from the sliding panel they can still surf around all the main site pages. When they log out the session dies and it reverts to the home page. However, when they are logged in I would like the sliding login panel to disappear from the top of each page. Is there any way I can do this simply? Any help or advice would be greatly appreciated. I have dynamic images that have the "Like" button, it's basically like a wishlist. The way I want it to work is that when a user is not logged in, the 'Like' button will navigate them to a login popup (which I already made). Looking to INSERT a row in a data table and keeping the User on the current page. Will it work like what I have below, or do I need to send it to its own page then return the User back to where he started? What I have below isn't inserting a row. No errors showing. function bookmark_add () { if (isset($_POST['submit'])) { include("/home2/csi/public_html/resources/con.php"); $query = "INSERT INTO a_player_bookmark (bookmark) VALUES ('1')"; $results = mysqli_query($con,$query); echo mysqli_error($con); } $output = '<form name="bookmark_add" method="post" action="" class="bookmark-plus">'; $output .= '<span><input type="submit" name="Bookmark"></span>'; $output .= '</form>'; return $output; }
So I'm making an ordering form for pizza, and it works fine and im using two separate pages: 1- display the form where the user can select the pizza, and 2-display the order and the price, and user can go back and edit the problem im having is when i got back from the 2nd page to the 1st page, to edit the data, I want the variables to stay the same. For example, if the user selected a medium cheese pizza, i want the values of cheese and pizza to still be there, so the user doesnt have to fill the entire form out again. ive done this before, but now when i try it doesn''t work and Ive tried everything. Thanks in advance If the code is needed, I'll post it here Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <title>Title of the document</title> <meta name="description" content="Type a Short Description Here" /> <meta name="keywords" content="type, keywords, here" /> <meta name="author" content="Your Name" /> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> <body> <?php print_r ($_REQUEST) ; if(isset($_POST['ORDER'])) { $ORDER = $_POST['ORDER']; } if(isset($_POST['PTYPE'])) { $PTYPE = $_POST['PTYPE']; } if(isset($_POST['DTYPE'])) { $DTYPE = $_POST['DTYPE']; } if(isset($_POST['PSIZE'])) { $PSIZE = $_POST['PSIZE']; } if(isset($_POST['DSIZE'])) { $DSIZE = $_POST['DSIZE']; } if(isset($_POST['PTYPE2'])) { $PTYPE2 = $_POST['PTYPE2']; } if(isset($_POST['DTYPE2'])) { $DTYPE2 = $_POST['DTYPE2']; } if(isset($_POST['PSIZE2'])) { $PSIZE2 = $_POST['PSIZE2']; } if(isset($_POST['DSIZE2'])) { $DSIZE2 = $_POST['DSIZE2']; } if(isset($_POST['PTYPE3'])) { $PTYPE3 = $_POST['PTYPE3']; } if(isset($_POST['DTYPE3'])) { $DTYPE3 = $_POST['DTYPE3']; } if(isset($_POST['PSIZE3'])) { $PSIZE3 = $_POST['PSIZE3']; } if(isset($_POST['DSIZE3'])) { $DSIZE3 = $_POST['DSIZE3']; } echo "Order Type: <br/> \n"; echo"<form method='post' action='project2.php'>"; }if ($ORDER=='delivery') { $DEL='checked' ; }elseif ($ORDER=='takeout') { $TAKE='checked'; }elseif ($ORDER=='eatin') { $EAT='checked'; } echo"Delivery <input type='radio' name='ORDER' value='delivery' $DEL> \n"; echo"Take Out <input type='radio' name='ORDER' value='takeout' $TAKE> \n"; echo"Eat in <input type='radio' name='ORDER' value='eatin' $EAT> \n"; if ($PTYPE==='no pizza') { $NOPIZZA='selected' ; }elseif ($PTYPE=='cheese') { $CHEESE='selected' ; }elseif ($PTYPE=='pepporoni') { $PEPPORONI='selected' ; }elseif ($PTYPE=='veggie') { $VEGGIE='selected' ; }elseif ($PTYPE=='meat lovers') { $MEAT='selected' ; } echo $CHEESE ; echo"<br/>Pizza: <select name='PTYPE'>\n"; echo"<option></option>"; echo"<option $NOPIZZA>no pizza</option>"; echo"<option $CHEESE>cheese</option>"; echo"<option $PEPPORONI>pepporoni</option>"; echo"<option $VEGGIE>veggie</option>"; echo"<option $MEAT>meat lovers</option>"; echo"</select> "; $SMALL='selected' ; if (isset ($_POST['PSIZE']) && ($PSIZE=='Small')) { $SMALL='selected' ; }elseif (isset ($_POST['PSIZE']) && ($PSIZE=='Medium')) { $MEDIUM='selected' ; }elseif (isset ($_POST['PSIZE']) && ($PSIZE=='Large')) { $LARGE='selected' ; } echo "$SMALL"; echo" Pizza size: <select name='PSIZE'>\n"; echo"<option></option>"; echo"<option $SMALL>Small</option>"; echo"<option $MEDIUM>Medium</option>"; echo"<option $LARGE>Large</option>"; echo"</select>"; echo"<br/>Pizza2: <select name='PTYPE2'>\n"; echo"<option></option>"; echo"<option >no pizza</option>"; echo"<option >cheese</option>"; echo"<option >pepporoni</option>"; echo"<option >veggie</option>"; echo"<option >meat lovers</option>"; echo"</select> "; echo" Pizza size2: <select name='PSIZE2'>\n"; echo"<option></option>"; echo"<option>Small</option>"; echo"<option>Medium</option>"; echo"<option>Large</option>"; echo"</select>"; echo"<br/>Pizza3: <select name='PTYPE3'>\n"; echo"<option></option>"; echo"<option >no pizza</option>"; echo"<option >cheese</option>"; echo"<option >pepporoni</option>"; echo"<option >veggie</option>"; echo"<option >meat lovers</option>"; echo"</select> "; echo"Pizza size3: <select name='PSIZE3'>\n"; echo"<option></option>"; echo"<option>Small</option>"; echo"<option>Medium</option>"; echo"<option>Large</option>"; echo"</select>"; echo"</br> <br/>What drink would you like? <select name='DTYPE'>"; echo"<option></option>"; echo"<option>No drink</option>"; echo"<option>Bottled Pepsi</option>"; echo"<option>Bottled Fanta</option>"; echo"<option>Bottled Sprite</option>"; echo"<option>Bottled RootBeer</option>"; echo"</select>"; echo"Drink size: <select name='DSIZE'>"; echo"<option></option>"; echo"<option>.5 liter</option>"; echo"<option>1 liter</option>"; echo"<option>2 liter</option>"; echo"</select>"; echo"</br> <br/>What second drink would you like? <select name='DTYPE2'>"; echo"<option></option>"; echo"<option>No drink</option>"; echo"<option>Bottled Pepsi</option>"; echo"<option>Bottled Fanta</option>"; echo"<option>Bottled Sprite</option>"; echo"<option>Bottled RootBeer</option>"; echo"</select>"; echo"Drink size2: <select name='DSIZE2'>"; echo"<option></option>"; echo"<option>.5 liter</option>"; echo"<option>1 liter</option>"; echo"<option>2 liter</option>"; echo"</select>"; echo"</br> <br/>What third drink would you like? <select name='DTYPE3'>"; echo"<option></option>"; echo"<option>No drink</option>"; echo"<option>Bottled Pepsi</option>"; echo"<option>Bottled Fanta</option>"; echo"<option>Bottled Sprite</option>"; echo"<option>Bottled RootBeer</option>"; echo"</select>"; echo"Drink size3: <select name='DSIZE3'>"; echo"<option></option>"; echo"<option>.5 liter</option>"; echo"<option>1 liter</option>"; echo"<option>2 liter</option>"; echo"</select>"; echo"<br/> <br/> <input type='submit' value='submit your order' name='submit'>"; echo"</form>"; ?> </body> </html> and the second page Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <title>Title of the document</title> <meta name="description" content="Type a Short Description Here" /> <meta name="keywords" content="type, keywords, here" /> <meta name="author" content="Your Name" /> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> <body> <?php print_r ($_REQUEST) ; $ORDER = $_POST['ORDER']; $PTYPE = $_POST['PTYPE']; $DTYPE = $_POST['DTYPE']; $PSIZE = $_POST['PSIZE']; $DSIZE = $_POST['DSIZE']; $PTYPE2 = $_POST['PTYPE2']; $DTYPE2 = $_POST['DTYPE2']; $PSIZE2 = $_POST['PSIZE2']; $DSIZE2 = $_POST['DSIZE2']; $PTYPE3 = $_POST['PTYPE3']; $DTYPE3 = $_POST['DTYPE3']; $PSIZE3 = $_POST['PSIZE3']; $DSIZE3 = $_POST['DSIZE3']; if (($PTYPE=='cheese')and($PSIZE=='Small')) { $PCOST=10 ; }elseif (($PTYPE=='cheese')and($PSIZE=='Medium')) { $PCOST=13 ; }elseif (($PTYPE=='cheese')and($PSIZE=='Large')) { $PCOST=15 ; }elseif (($PTYPE=='pepporoni')and($PSIZE=='Small')) { $PCOST=12 ; }elseif (($PTYPE=='pepporoni')and($PSIZE=='Medium')) { $PCOST=14 ; }elseif (($PTYPE=='pepporoni')and($PSIZE=='Large')) { $PCOST=16 ; }elseif (($PTYPE=='veggie')and($PSIZE=='Small')) { $PCOST=12 ; }elseif (($PTYPE=='veggie')and($PSIZE=='Medium')) { $PCOST=14 ; }elseif (($PTYPE=='veggie')and($PSIZE=='Large')) { $PCOST=16 ; }elseif (($PTYPE=='meat lovers')and($PSIZE=='Small')) { $PCOST=13 ; }elseif (($PTYPE=='meat lovers')and($PSIZE=='Medium')) { $PCOST=15 ; }elseif (($PTYPE=='meat lovers')and($PSIZE=='Large')) { $PCOST=17; }else { $PCOST=0 ; } if (($PTYPE2=='cheese')and($PSIZE2=='Small')) { $PCOST2=10 ; }elseif (($PTYPE2=='cheese')and($PSIZE2=='Medium')) { $PCOST2=13 ; }elseif (($PTYPE2=='cheese')and($PSIZE2=='Large')) { $PCOST2=15 ; }elseif (($PTYPE2=='pepporoni')and($PSIZE2=='Small')) { $PCOST2=12 ; }elseif (($PTYPE2=='pepporoni')and($PSIZE2=='Medium')) { $PCOST2=14 ; }elseif (($PTYPE2=='pepporoni')and($PSIZE2=='Large')) { $PCOST2=16 ; }elseif (($PTYPE2=='veggie')and($PSIZE2=='Small')) { $PCOST2=12 ; }elseif (($PTYPE2=='veggie')and($PSIZE2=='Medium')) { $PCOST2=14 ; }elseif (($PTYPE2=='veggie')and($PSIZE2=='Large')) { $PCOST2=16 ; }elseif (($PTYPE2=='meat lovers')and($PSIZE2=='Small')) { $PCOST2=13 ; }elseif (($PTYPE2=='meat lovers')and($PSIZE2=='Medium')) { $PCOST2=15 ; }elseif (($PTYPE2=='meat lovers')and($PSIZE2=='Large')) { $PCOST2=17; }else { $PCOST2=0 ; } if (($PTYPE3=='cheese')and($PSIZE3=='Small')) { $PCOST3=10 ; }elseif (($PTYPE3=='cheese')and($PSIZE3=='Medium')) { $PCOST3=13 ; }elseif (($PTYPE3=='cheese')and($PSIZE3=='Large')) { $PCOST3=15 ; }elseif (($PTYPE3=='pepporoni')and($PSIZE3=='Small')) { $PCOST3=12 ; }elseif (($PTYPE3=='pepporoni')and($PSIZE3=='Medium')) { $PCOST3=14 ; }elseif (($PTYPE3=='pepporoni')and($PSIZE3=='Large')) { $PCOST3=16 ; }elseif (($PTYPE3=='veggie')and($PSIZE3=='Small')) { $PCOST3=12 ; }elseif (($PTYPE3=='veggie')and($PSIZE3=='Medium')) { $PCOST3=14 ; }elseif (($PTYPE3=='veggie')and($PSIZE3=='Large')) { $PCOST3=16 ; }elseif (($PTYPE3=='meat lovers')and($PSIZE3=='Small')) { $PCOST3=13 ; }elseif (($PTYPE3=='meat lovers')and($PSIZE3=='Medium')) { $PCOST3=15 ; }elseif (($PTYPE3=='meat lovers')and($PSIZE3=='Large')) { $PCOST3=17; }else { $PCOST3=0 ; } if((($DTYPE=='Bottled Pepsi')or($DTYPE=='Bottled Fanta')or($DTYPE=='Bottled Sprite')or($DTYPE=='Bottled RootBeer'))and($DSIZE=='.5 liter')) { $DCOST=.69 ; }elseif((($DTYPE=='Bottled Pepsi')or($DTYPE=='Bottled Fanta')or($DTYPE=='Bottled Sprite')or($DTYPE=='Bottled RootBeer'))and($DSIZE=='1 liter')) { $DCOST=.99 ; }elseif((($DTYPE=='Bottled Pepsi')or($DTYPE=='Bottled Fanta')or($DTYPE=='Bottled Sprite')or($DTYPE=='Bottled RootBeer'))and($DSIZE=='2 liter')) { $DCOST=1.69 ; }elseif($DTYPE=='No drink'){ $DCOST=0 ; } if((($DTYPE2=='Bottled Pepsi')or($DTYPE2=='Bottled Fanta')or($DTYPE2=='Bottled Sprite')or($DTYPE2=='Bottled RootBeer'))and($DSIZE2=='.5 liter')) { $DCOST2=.69 ; }elseif((($DTYPE2=='Bottled Pepsi')or($DTYPE2=='Bottled Fanta')or($DTYPE2=='Bottled Sprite')or($DTYPE2=='Bottled RootBeer'))and($DSIZE2=='1 liter')) { $DCOST2=.99 ; }elseif((($DTYPE2=='Bottled Pepsi')or($DTYPE2=='Bottled Fanta')or($DTYPE2=='Bottled Sprite')or($DTYPE2=='Bottled RootBeer'))and($DSIZE2=='2 liter')) { $DCOST2=1.69 ; }elseif($DTYPE2=='No drink'){ $DCOST2=0 ; } if((($DTYPE3=='Bottled Pepsi')or($DTYPE3=='Bottled Fanta')or($DTYPE3=='Bottled Sprite')or($DTYPE3=='Bottled RootBeer'))and($DSIZE3=='.5 liter')) { $DCOST3=.69 ; }elseif((($DTYPE3=='Bottled Pepsi')or($DTYPE3=='Bottled Fanta')or($DTYPE3=='Bottled Sprite')or($DTYPE3=='Bottled RootBeer'))and($DSIZE3=='1 liter')) { $DCOST3=.99 ; }elseif((($DTYPE3=='Bottled Pepsi')or($DTYPE3=='Bottled Fanta')or($DTYPE3=='Bottled Sprite')or($DTYPE3=='Bottled RootBeer'))and($DSIZE3=='2 liter')) { $DCOST3=1.69 ; }elseif($DTYPE3=='No drink'){ $DCOST3=0 ; } $TOTAL=($PCOST+$PCOST3+$PCOST3+$DCOST+$DCOST2+$DCOST3) ; echo "<br/><br/>Your Order is:<br/>" ; echo " $PSIZE $PTYPE ($PCOST),$PSIZE2 $PTYPE2 ($PCOST2),$PSIZE3 $PTYPE3 ($PCOST3), <br/>" ; echo " $DSIZE $DTYPE ($DCOST),$DSIZE2 $DTYPE2 ($DCOST2),$DSIZE3 $DTYPE3 ($DCOST3), <br/> <br/>" ; echo"<br/><br/>Your total price is: $TOTAL <br/> <br/>"; if ($ORDER=='delivery') { echo"Please enter the information required for delivery <br/>" ; echo "Address: <input type=text><br/>"; echo "City: <input type=text><br/>"; echo "Zip Code: <input type=text><br/>"; } elseif ($ORDER=='takeout') { echo "Your order will be ready for pickup in 30 min"; } elseif ($ORDER=='eatin') { echo "Your order will be ready in 30 min"; } echo"<form action=asdf.php>"; echo"<br/> <input type=submit value='confirm this order'>"; echo"</form>"; echo "<form action=project.php>"; echo "<br/> <br/><input type=submit value='edit this order'>"; echo "<input type='hidden' name='ORDER' value='$ORDER'>"; echo "<input type='hidden' name='PTYPE' value='$PTYPE'>"; echo "<input type='hidden' name='PTYPE2' value='$PTYPE2'>"; echo "<input type='hidden' name='PTYPE3' value='$PTYPE3'>"; echo "<input type='hidden' name='PSIZE' value='$PSIZE'>"; echo "<input type='hidden' name='PSIZE2' value='$PSIZE2'>"; echo "<input type='hidden' name='PSIZE3' value='$PSIZE3'>"; echo "<input type='hidden' name='DTYPE' value='$DTYPE'>"; echo "<input type='hidden' name='DTYPE2' value='$DTYPE2'>"; echo "<input type='hidden' name='DTYPE3' value='$DTYPE3'>"; echo "<input type='hidden' name='DSIZE' value='$DSIZE'>"; echo "<input type='hidden' name='DSIZE2' value='$DSIZE2'>"; echo "<input type='hidden' name='DSIZE3' value='$DSIZE3'>"; echo "</form>"; ?> </body> </html> This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=316188.0 Alright, so I'm fairly new to PHP coding and I still have a ton to learn, so it's not surprising that I ran into a problem pretty quickly. I've setup a database and even managed to scrap together a SIMPLE member management system. All of it works, but I still need one thing. A lot of sites I visit which allow users to signup have this at the top; Login or Register. Nothing huge, just in the corner, know what I mean? I was wondering how I do this? Also, after someone logs in, how do I change that to show "You are logged in as [username] and then a logout option? Hello all, This has probably been asked before but I couldn't find through search. And I'm pretty sure is not possible, but... Is there a way of displaying the windows logged on user? or get the name of the compter? as I know I can get the ip address. The reason I ask. Each user has their own network account but on occasion we need to log on a user as a generic account we have. And I want to check if it's this user accessing the page so different options etc can be displayed. I could use the computer name to check this as we log all activity. I could then query the log using the comp name to find logged on user. Unfortunately the log does not hold ip, which would seem obvious but it doesn't Many Thanks Hi guys, Have a wierd scenario where my brain is simply refusing to think. I have small script which let's a user log in through a login screen. Thats ok. I can check whether the user is already logged in the same machine through sessions. My requirement is : I need to restrict the same username logging in from multiple computers at any one given time. In other words, a user can not use two machines to login at the same time. Can someone please suggest a method to solve this scenario. Your help is very much appreciated. Cheers Elabuwa Hi i am wondering how i can redirect someone that's not logged in?
I am making a login system and I have done it before. ryanweekly.com is the site I am trying to put it on. Here is the script to the home page. Here is the code I am having a problem with: Code: [Select] <?php if (empty($username)) include ("loginbar.php"); if (!empty($username)) include ("logged.php"); ?> Code: [Select] <?PHP $username = $_SESSION['MM_Username']; ?> <?php require_once('Connections/ryanweeklyusers.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } ?> <?php // *** Validate request to login to this site. if (!isset($_SESSION)) { session_start(); } $loginFormAction = $_SERVER['PHP_SELF']; if (isset($_GET['accesscheck'])) { $_SESSION['PrevUrl'] = $_GET['accesscheck']; } if (isset($_POST['username'])) { $loginUsername=$_POST['username']; $password=$_POST['password']; $MM_fldUserAuthorization = ""; $MM_redirectLoginSuccess = "index.php"; $MM_redirectLoginFailed = "index.php?reg=loger"; $MM_redirecttoReferrer = false; mysql_select_db($database_ryanweeklyusers, $ryanweeklyusers); $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); $LoginRS = mysql_query($LoginRS__query, $ryanweeklyusers) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = ""; if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();} //declare two session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserGroup'] = $loginStrGroup; if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; } header("Location: " . $MM_redirectLoginSuccess ); } else { header("Location: ". $MM_redirectLoginFailed ); } } $ruser = $_REQUEST['Y2User-53201']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Ryan Weekly</title> <style type="text/css"> body { background-color: #E3E5E2; } </style> <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script> <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" /> </head> <style type="text/css"> <!-- body { font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif; } div#wrapper { width: 80%; background-color:#FFFFFF; margin-top: 50px; margin-bottom: 50px; margin-left: auto; margin-right: auto; padding: 0px; border: thin solid #000000; } div#header { padding: 15px; margin: 0px; text-align: center; } div#nav { width: 25%; padding: 10px; margin-top: 1px; float: left; border: thin solid #000000; } div#main { margin-left: 30%; margin-top: 1px; padding: 10px; border: thin solid #000000; } div#underhead { padding: 15px; margin: 0px; border-top: thin solid #000000; } { ul.nav a:hover, ul.nav a:active, ul.nav a:focus { /* this changes the background and text color for both mouse and keyboard navigators */ background: #6F7D94; color: #FFF; } .content ul, .content ol { padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */ } /* ~~ The navigation list styles (can be removed if you choose to use a premade flyout menu like Spry) ~~ */ ul.nav { list-style: none; /* this removes the list marker */ border-top: 1px solid #666; /* this creates the top border for the links - all others are placed using a bottom border on the LI */ margin-bottom: 15px; /* this creates the space between the navigation on the content below */ } ul.nav li { border-bottom: 1px solid #666; /* this creates the button separation */ } ul.nav a, ul.nav a:visited { /* grouping these selectors makes sure that your links retain their button look even after being visited */ padding: 5px 5px 5px 15px; display: block; /* this gives the link block properties causing it to fill the whole LI containing it. This causes the entire area to react to a mouse click. */ text-decoration: none; background: #8090AB; color: #000; } ul.nav a:hover, ul.nav a:active, ul.nav a:focus { /* this changes the background and text color for both mouse and keyboard navigators */ background: #6F7D94; color: #FFF; } /* ~~ The footer ~~ */ .footer { padding: 10px 0; background: #6F7D94; position: relative;/* this gives IE6 hasLayout to properly clear */ clear: both; /* this clear property forces the .container to understand where the columns end and contain them */ } /* ~~ miscellaneous float/clear classes ~~ */ .fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */ float: right; margin-left: 8px; } .fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */ float: left; margin-right: 8px; } .clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */ clear:both; height:0; font-size: 1px; line-height: 0px; } --> </style> <body> <div align="Left"> <p><img src="indexp.png" width="728" height="90" /></p> </div> <div id="underhead"> <?php if (empty($username)) include ("loginbar.php"); if (!empty($username)) include ("logged.php"); ?> <?PHP $justreg = $_REQUEST['reg']; if ($justreg == yes) echo 'Thanks For Registering!'; if ($justreg == loger) echo 'There was an error while tring to log you in.'; if ($justreg == out) echo 'You were logged out!'; ?> </div> <?php include("menu.php"); ?> <div id="main"> <p>Welcome To Ryan Weekly! The all new and improved site!</p> <p>I have a song on itunes!</p> <p> <object width="400" height="160"> <param name="movie" value="http://widget.tunecore.com/swf/tc_run_h_v2.swf?widget_id=62316" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /> <embed src="http://widget.tunecore.com/swf/tc_run_h_v2.swf?widget_id=62316" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="160"></embed> </object> </p> </div> Ryan Weekly 2009 - 2011 (May 11, 2011 at 6:00PM Is our 3 Year anniversary!) <script type="text/javascript"> var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1"); var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2"); </script> </body> </html> Hi there,
I've been searching the internet for the best way to check if the user has been logged in. Some codes have security breaches. So I'm not sure where to start.
Here's what I've come up with:
The user logs in and is checked whether he/she is a valid user, if not return false and if true carry on and create session, I read the post that Jacques1 made about session feedback and implemented what he said. After that the session variables are assigned and then the user id, session_id and a unique identifier to check against on each page load are inserted into a database and then the user is logged in.
Here's my code: (please note this is in a class and only shows the login function)
function Login($username, $password) { try { $db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8", DB_USERNAME, DB_PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch(PDOException $ex) { echo "Unable to connect to DB"; error_log($ex->getMessage()); } try { $User_Info = $db->prepare("SELECT * FROM users WHERE username=:username"); $User_Info->bindValue(":username", $username, PDO::PARAM_STR); $User_Info->execute(); $Info = $User_Info->fetchAll(PDO::FETCH_ASSOC); $salt = $Info['salt']; $password = $salt . $password; $password = $this->CreateHash($password); $unique_key = $this->GenerateRandom(); $unique_key = $this->CreateHash($unique_key); $Check_User = $db->prepare("SELECT * FROM users WHERE username=:username AND password=:password"); $Check_User->bindValue(":username", $username, PDO::PARAM_STR); $Check_User->bindValue(":password", $password, PDO::PARAM_STR); $Check_User->execute(); if($Check_User->rowCount() > 0) { while($row = $Check_User->fetchAll(PDO::FETCH_ASSOC)) { session_destroy(); session_start(); $_SESSION = array(); session_regenerate_id(true); $_SESSION['username'] = $row['username']; $session_id = session_id(); $user_id = $row['id']; $Check_Logged_In = $db->prepare("DELETE FROM logged_in_users WHERE user_id=:userid"); $Check_Logged_In->bindValue(":user_id", $user_id, PDO::PARAM_STR); $Check_Logged_In->execute(); $has_changed = $Check_Logged_In->rowCount(); if($has_changed > 0) { $Logged_In = $db->prepare("INSERT INTO logged_in_users (id, user_id, session_id, unique_key) VALUES (NULL, :user_id, :session_id, :unique_key)"); $Logged_In->bindValue(":user_id", $user_id, PDO::PARAM_STR); $Logged_In->bindValue(":session_id", $session_id, PDO::PARAM_STR); $Logged_In->bindValue(":unique_key", $unique_key, PDO::PARAM_STR); $Logged_In->execute(); $affected_rows = $Logged_In->rowCount(); if($affected_rows > 0) { return true; } } return false; } } return false; } catch(PDOException $ex) { echo "Unable to complete query"; error_log($ex->getMessage()); } }Thanks What are the different ways you can keep a User "logged in"? From what I *vaguely* recall from a year or two ago when I read a whole hoard of PHP books, you commonly use cookies and sessions. But I'm asking this more from an OOP standpoint than a PHP standpoint. Let's say I have a User record in my database, and a User comes along and attempts to log in. In OOP terms, I would think you'd call some class to help log them in, and upon successfully logging in, you would "load" the User object into memory and set the "LoggedIn" field to "True". Then as long as that field was set in their object, they could surf all over the place and do things like change their account and buy things. Is that how you would do it in OOP? TomTees hello i am stuck and hoping you can help, i have created this function and its not working correctly, what i am trying to do is when someone is logged in they get this line of code echo "<a href=\"update-content.php?id=5\">Edit</a> | <a href=\"delete-content.php?id=5\">Delete</a>"; which is ok as i dont want that to work yet and if your not logged in you dont get it. i have added //show when logged in and show not logged in so you know which code i want it to load when at the moment its only loading the not logged in one even if you are. i have looked on google read up on php sites but cant figure out how to do it, so hoping you guys will tell me were i gone wrong and how to correct it, thanks again heres the code. function get_left() { if (isset($_GET['page'])) { $page = $_GET['page']; //Show Page not logged in $sql = "SELECT title,body FROM content WHERE page = '$page' AND location = 'left' AND disabled = 'no' ORDER BY id"; $res = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($res)){ echo '<h1>' . $row['title'] . '</h1>'; echo '<p>' . $row['body'] . '</p>'; } } //show when logged in elseif ($_SESSION['username']&&(isset($_GET['page']))){ $sql = "SELECT title,body FROM content WHERE page = '$page' AND location = 'left' AND disabled = 'no' ORDER BY id"; $res = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($res)){ echo '<h1>' . $row['title'] . '</h1>'; echo '<p>' . $row['body'] . '</p>'; echo "<a href=\"update-content.php?id=5\">Edit</a> | <a href=\"delete-content.php?id=5\">Delete</a>"; } } } In the site I am making, the client wants one account for everyone (don't ask me why, it's a long, silly reason) And I'm wondering how I would configure mySQL to deal with that (I asked this in the mySQL area) and in PHP (For you guys ) what would I need to put in order for this to work. I'm very new with PHP and mySQL, so please forgive the nooby question. *EDIT* the admins have their own username and psw for adding, deleting, and editing articles. and that works. But all users that register will have this set user and psw displayed to them. I want to make it so they have to log in to see an article. Hi, I want have this code (below), how would I check if a user is logged in? I want to make it so they can only see 500 chars, or the full thing if they're logged in. Thanks! Code: [Select] public function __construct( $data=array() ) { if ( isset( $data['id'] ) ) $this->id = (int) $data['id']; if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate']; if ( isset( $data['title'] ) ) $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] ); if ( isset( $data['summary'] ) ) $this->summary = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary'] ); if ( isset( $data['content'] ) ) $this->content = $data['content']; if ( isset( $data['tags'] ) ) $this->tags = $data['tags']; } Sites such as this one often show the logged on users and guests.
I have no reason to need to do so, but am curious on how this is accomplished.
For users, yes, you've authenticated them and logged them on regardless of IP address, but how do you know they didn't just close their browser?
For guests, are they just using IP address? And still, how do you know when they leave?
PS. How should I include an image in a post like I did? What I did was first attach a file, and then edit the post to include that file as an image. Couldn't seem to include an image off my local PC. Not a better way?
Attached Files
Capture.PNG 4.13KB
0 downloads Hi all, Does anyone know of an effective way of find out whether a user is still logged in and they haven't left? Sam on my site: http://www.retroandvintage.co.uk whne you login ast test/test teh search does not work?! search page code Code: [Select] <?php session_start(); include_once("config.php"); $ebits = ini_get('error_reporting'); error_reporting($ebits ^ E_NOTICE); // Check user logged in already: checkLoggedIn("yes"); ?> <!doctype html> <html> <head> <title>Pubs and bars in <? echo $Townsearch;?></title> <meta name="description" content="Pubs, bars and restuarants in <? echo $Townsearch;?>" /> <meta name="keywords" content="<? echo $Townsearch;?>, pubs, bars, food, drink, nightlife" /> <meta name="Content-Language" content="en-gb" /> <meta name="robots" content="FOLLOW,INDEX" /> <meta name="revisit-after" content="2 days" /> <meta name="copyright" content="jbiddulph.com" /> <meta name="author" content="John Biddulph - Professional web site design and development in the south of england mainly worthing and brighton" /> <meta name="distribution" content="Global" /> <meta name="resource-type" content="document" /> <link rel="stylesheet" type="text/css" href="css/reset.css" /> <link rel="stylesheet" type="text/css" href="css/style.css" title="default" /> <link rel="alternate stylesheet" type="text/css" href="css/style1.css" title="1" /> <link rel="alternate stylesheet" type="text/css" href="css/style2.css" title="2" /> <script type="text/javascript" src="js/stylechanger.js"></script> <script type="text/javascript" src="js/jquery-1.2.1.pack.js"></script> <script type="text/javascript"> function lookup(inputString) { if(inputString.length == 0) { // Hide the suggestion box. $('#suggestions').hide(); } else { $.post("rpc.php", {queryString: ""+inputString+""}, function(data){ if(data.length >0) { $('#suggestions').show(); $('#autoSuggestionsList').html(data); } }); } } // lookup function fill(thisValue) { $('#inputString').val(thisValue); setTimeout("$('#suggestions').hide();", 200); } </script> </head> <body> <?php if($messages) { displayErrors($messages); }?> <header> <div id="title"> <h1>My Pub Space <a href="#" onClick="setActiveStyleSheet('default'); return false;"><img src="images/0.gif" width="15" height="15" border="0" alt="css style" /></a> <a href="#" onClick="setActiveStyleSheet('1'); return false;"><img src="images/1.gif" width="15" height="15" border="0" alt="css style" /></a> <a href="#" onClick="setActiveStyleSheet('2'); return false;"><img src="images/2.gif" width="15" height="15" border="0" alt="css style" /></a> <span> <form method="post" class="textbox" action="search.php?rsTown=<? echo $Town ?>&rsCounty=<? echo $County ?>"> Town/City: <input type="text" size="26" class="searchbox" value="" name="rsTown" id="inputString" onKeyUp="lookup(this.value);" onBlur="fill();" /> <input type="hidden" value="<? echo $County ?>" name="rsCounty" id="inputString" onKeyUp="lookup(this.value);" onBlur="fill();" /> <div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="images/upArrow.png" style="position: relative; top: -36px; left: 105px; z-index:1;" alt="upArrow" /> <div class="suggestionList" id="autoSuggestionsList"> </div> </div> <input type="image" src="images/go.png" height="30" with="30" value="GO" /> </form> </span> </h1> </div> </header> <nav> <ul> <li class="selected"><a href="#">Home</a></li> <li><a href="#">Pubs</a></li> <li><a href="#">Members</a></li> <li><a href="#">Events</a></li> <li><a href="#">Register</a></li> </ul> </nav> <section id="intro"> <header> <h2>Your social guide to going down the pub, online!</h2> </header> <p>Stuck in town with nowhere to go? Not sure if up the road or down the street is best? Need to be somewhere warm, cosy and friendly. Need a drink?....<br />You've come to the right place, mypubspace has it all!</p> <img src="images/pub.jpg" alt="pub" /> </section> <div id="content"> <div id="mainContent"> <section> <article class="blogPost"> <header> <h2>Pubs in <? echo $Town;?>, <? $_SESSION['county'];?></h2> <p>Posted on <time datetime="2009-06-29T23:31+01:00">June 29th 2009</time> by <a href="#">Mads Kjaer</a> - <a href="#comments">3 comments</a></p> </header> <?php $tableName="pubs"; $targetpage = "search.php"; $limit = 20; $query = "SELECT COUNT(*) as num FROM $tableName WHERE rsTown LIKE '$Town%'"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages['num']; $stages = 3; $page = $_GET['page']; if($page){ $start = ($page - 1) * $limit; }else{ $start = 0; } // Get page data $query1 = "SELECT * FROM $tableName WHERE rsTown LIKE '$Town%' LIMIT $start, $limit"; $result = mysql_query($query1); // Initial page num setup if ($page == 0){$page = 1;} $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages/$limit); $LastPagem1 = $lastpage - 1; $paginate = ''; if($lastpage > 1) { $paginate .= "<div class='paginate'>"; // Previous if ($page > 1){ $paginate.= "<a href='$targetpage?page=$prev&rsTown=$Town'>previous</a>"; }else{ $paginate.= "<span class='disabled'>previous</span>"; } // Pages if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter&rsTown=$Town&rsCounty=$County'>$counter</a>";} } } elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few? { // Beginning only hide later pages if($page < 1 + ($stages * 2)) { for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter&rsTown=$Town&rsCounty=$County'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$LastPagem1&rsTown=$Town&rsCounty=$County'>$LastPagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage&rsTown=$Town&rsCounty=$County'>$lastpage</a>"; } // Middle hide some front and some back elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { $paginate.= "<a href='$targetpage?page=1&rsTown=$Town&rsCounty=$County'>1</a>"; $paginate.= "<a href='$targetpage?page=2&rsTown=$Town&rsCounty=$County'>2</a>"; $paginate.= "..."; for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter&rsTown=$Town&rsCounty=$County'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$LastPagem1&rsTown=$Town&rsCounty=$County'>$LastPagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage&rsTown=$Town&rsCounty=$County'>$lastpage</a>"; } // End only hide early pages else { $paginate.= "<a href='$targetpage?page=1&rsCounty=$County'>1</a>"; $paginate.= "<a href='$targetpage?page=2&rsCounty=$County'>2</a>"; $paginate.= "..."; for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter&rsTown=$Town&rsCounty=$County'>$counter</a>";} } } } // Next if ($page < $counter - 1){ $paginate.= "<a href='$targetpage?page=$next&rsTown=$Town&rsCounty=$County'>next</a>"; }else{ $paginate.= "<span class='disabled'>next</span>"; } $paginate.= "</div>"; } echo $total_pages.' Results'; // pagination echo $paginate; ?> <ul> <?php while($row = mysql_fetch_array($result)) { echo '<li>'.$row['rsPubName'].', '.$row['rsTown'].', '.$row['rsCounty'].'</li>'; } $_SESSION['county'] = $row['rsCounty']; ?> </ul> </article> </section> <section id="comments"> <h3>Comments</h3> <article> <header> <a href="#">George Washington</a> on <time datetime="2009-06-29T23:35:20+01:00">June 29th 2009 at 23:35</time> </header> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p> </article> <article> <header> <a href="#">Benjamin Franklin</a> on <time datetime="2009-06-29T23:40:09+01:00">June 29th 2009 at 23:40</time> </header> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p> </article> <article> <header> <a href="#">Barack Obama</a> on <time datetime="2009-06-29T23:59:00+01:00">June 29th 2009 at 23:59</time> </header> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut.</p> </article> </section> <form action="" method="POST" method="post"> <h3>Post a comment</h3> <p> <label for="name">Name</label> <input name="name" id="name" type="text" required /> </p> <p> <label for="email">E-mail</label> <input name="email" id="email" type="email" required /> </p> <p> <label for="website">Website</label> <input name="website" id="website" type="url" /> </p> <p> <label for="comment">Comment</label> <textarea name="comment" id="comment" required></textarea> </p> <p> <input type="submit" value="Post comment" /> </p> </form> </div> <aside> <section> <header> <h3>Members Login Area</h3> </header> <h4>Welcome <? print($_SESSION["rsUser"]); ?></h4> <ul> <li><a href="#">Sign up</a></li> <li><a href="#">Forgot Password</a></li> </ul> </section> <section> <header> <h3>Archives</h3> </header> <ul> <li><a href="#">December 2008</a></li> <li><a href="#">January 2009</a></li> <li><a href="#">February 2009</a></li> <li><a href="#">March 2009</a></li> <li><a href="#">April 2009</a></li> <li><a href="#">May 2009</a></li> <li><a href="#">June 2009</a></li> </ul> </section> </aside> </div> <footer> <div> <section id="about"> <header> <h3>About</h3> </header> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco <a href="#">laboris nisi ut aliquip</a> ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </section> <section id="blogroll"> <header> <h3>Blogroll</h3> </header> <ul> <li><a href="#">NETTUTS+</a></li> <li><a href="#">FreelanceSwitch</a></li> <li><a href="#">In The Woods</a></li> <li><a href="#">Netsetter</a></li> <li><a href="#">PSDTUTS+</a></li> </ul> </section> <section id="popular"> <header> <h3>Popular</h3> </header> <ul> <li><a href="#">This is the title of a blog post</a></li> <li><a href="#">Lorem ipsum dolor sit amet</a></li> <li><a href="#">Consectetur adipisicing elit, sed do eiusmod</a></li> <li><a href="#">Duis aute irure dolor</a></li> <li><a href="#">Excepteur sint occaecat cupidatat</a></li> <li><a href="#">Reprehenderit in voluptate velit</a></li> <li><a href="#">Officia deserunt mollit anim id est laborum</a></li> <li><a href="#">Lorem ipsum dolor sit amet</a></li> </ul> </section> </div> </footer> </body> </html> |