chanfuterboy Posted August 6, 2009 Share Posted August 6, 2009 I need help with a protected php scripts. THis script should go above every member pages after login. I try to make one, but instead of go to 1st page protected it goes to login.php need help <?php session_start(); require_once("connect.php"); if(!isset($_SESSION["username"])) { header('Location: login.php'); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/ Share on other sites More sharing options...
Mardoxx Posted August 6, 2009 Share Posted August 6, 2009 WTH? did you use google translate or something?? Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892336 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 posting your login script would help, but make sure of a few things 1. that the session 'username' is the correct name of the session variable you set when a user logs in 2. that you actually set the username to a value. this is a very common problem Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892337 Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 Hi, im posting the login code. <?php // we must never forget to start the session session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); require_once("connect.php"); // check if the user id and password combination exist in database $query = "SELECT username FROM members WHERE username = '$username' AND password = '$password'"; $row = mysql_query($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892341 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 $_SESSION[username] = $row[username]; this is your problem. the $row variable holds a mysql resource, not a mysql associative array. to do that you need to do $array = mysql_fetch_assoc($row); $_SESSION['username'] = $array['username']; hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892345 Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 hi, but in what that would help with this below code help me on it, im just to blank to see the logic <?php session_start(); require_once("connect.php"); if(!isset($_SESSION["username"])) { header('Location: login.php'); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892350 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 the logic is that the session var isn't set to anything, because what you are trying to set it to doesn't exist. I'm suprised you don't get a syntax error on your log in page. session_start(); require_once("connect.php"); if(!isset($_SESSION["username"])) { header('Location: login.php'); exit; } ?> this is perfectly fine, the problem lies in your login script: <?php // we must never forget to start the session session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); require_once("connect.php"); // check if the user id and password combination exist in database $query = "SELECT username FROM members WHERE username = '$username' AND password = '$password'"; $row = mysql_query($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892353 Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 hi, Ok, now how i can put it a way short that i can protect my member pages? Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892356 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2009 Share Posted August 6, 2009 Check if the user is logged in (see the session variable) in the start of each protected page. And if they are not logged in redirect them to login.php. Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892358 Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 thats the thing after if(!isset($_SESSION['my_session_variable'])){ that session Variable im doubt on it what it is, following the login script Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892361 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 I don't really know what your saying at all... but you clearly don't understand session variables if you are having trouble understanding what the problem is with your script (especially since we explained it) OR you don't understand english very well (as evident by your speech) and can't really understand what i'm saying. Either try reading a tutorial on session variables and mysql results, or try a different, non-english board. best of luck Quote Link to comment https://forums.phpfreaks.com/topic/169125-protected-pages-help-needed/#findComment-892364 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.