chris17 Posted October 21, 2013 Share Posted October 21, 2013 Please the when ever i open signin.php which is the second code I get an error as a result "if($_SESSION['signed_in']){echo 'Hello' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>';} " in the first code saying that the variable _SESSION is undefined. Please what can i do about this? //header.php <!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" /> <meta name="description" content="A short Description." /> <meta name="keywords" content="put, keywords, here" /> <link rel="stylesheet" href="style.css" type="text/css" /> <title>PHP-MySQL Forum</title> </head> <body> <div id="wrapper"> <div id = "menu"> <a class="item" href="index.php">Index</a> <a class="item" href="create_topic.php">Create Topic</a> <a class="item" href="create_cat.php">Create Category</a> <?php echo '<div id="userbar">'; if($_SESSION['signed_in']) { echo 'Hello' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>'; } else { echo '<a href="signin.php">Sign in</a> or <a href="sign up">create an account</a>.'; } echo'</div>'; ?> </div> <div id="content"> <div id="footer">Created by Christech for Learning Only</div> </div> </div> </body> </html> <?php //signin.php include 'connect.php'; include 'header.php'; echo '<h3>Sign in</h3>'; //first, check if the user is already signed in. If that is the case, there is no need to display this page if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true) { echo 'You are alredy signed in, you can <a href="signout.php>Sign Out</a> if you wish"'; } else { if($_SERVER['REQUEST_METHOD'] != 'POST') { /*the form hasn't been posted yet, display it note that the action="" will cause the form to post to the same page it is on */ echo'<form method="post" action=""> Username: <input type="text" name="user_name" /> Password: <input type="text" name="user_pass" /> <input type="submit" value="Sign in" /> </form>'; } else { /* so, the form has been posted, we'll process the data in three steps: 1. Check the data 2. Let the user refill the wrong fields (if necessary) 3. Varify if the data is correct and return the correct response */ $errors = array(); /* declare the array for later use */ if(!isset($_POST['user_name'])) { $errors[] = 'The username field must not be empty.'; } if(!isset($_POST['user_pass'])) { $errors[] = 'The password field must not be empty.'; } if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/ { echo 'Uh-oh.. a couple of fields are not filled in correctly..'; echo '<ul>'; foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ { echo '<li>' . $value . '</li>'; /* this generates a nice error list */ } echo '</ul>'; } else { //the form has been posted without, so save it //notice the use of mysql_real_escape_string, keep everything safe! //also notice the sha1 function which hashes the password $sql = "SELECT user_id, user_name, user_level FROM users WHERE user_name = '". mysql_real_escape_string($_POST['user_name']) ."' AND user_pass = '". sha1($_POST['user_pass']) ."'"; $result = mysql_query($sql); if(!$result) { //something went wrong, display the error echo 'Something went wrong while signing in. Please try again later.'; //echo mysql_error(); //debugging purposes, uncomment when needed } else { //the query was successfully executed, there are 2 possibilities //1. the query returned data, the user can be signed in //2. the query returned an empty result set, the credentials were wrong if (mysql_num_rows($result) == 0){ echo'You have supplied a wrong user/password combination. Please try again'; } else { //set the $_SESSION['signed_in'] variable to TRUE $_SESSION['signed_in'] = true; //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages while($row = mysql_fetch_assoc($result)) { $_SESSION['user_id'] = $row['user_id']; $_SESSION['user_name'] = $row['user_name']; $_SESSION['user_level'] = $row['user_level']; } echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="index.php">Proceed to the forum overview</a>.'; } } } } } include 'footer.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/ Share on other sites More sharing options...
Barand Posted October 21, 2013 Share Posted October 21, 2013 you need to call session_start() at top of each page that uses $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1454858 Share on other sites More sharing options...
chris17 Posted October 22, 2013 Author Share Posted October 22, 2013 Please using my code, tell me how i can call this session. Thanks Boss Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1455013 Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 have you read this page http://uk3.php.net/session_start It explains and shows clearly how and where you call this function (look at the examples). Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1455016 Share on other sites More sharing options...
chris17 Posted October 22, 2013 Author Share Posted October 22, 2013 After starting the pages with session start, the following error came up: Notice: Undefined index: signed_in in C:\wamp\www\Unnamed Site 2\header.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1455017 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 22, 2013 Solution Share Posted October 22, 2013 (edited) Line 23 in header should be if(isset($_SESSION['signed_in'])) Edited October 22, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1455018 Share on other sites More sharing options...
chris17 Posted October 22, 2013 Author Share Posted October 22, 2013 U just saved me a learning problem. Am grateful. My name is Chris and it an honour learning from ya. Thanks Boss Quote Link to comment https://forums.phpfreaks.com/topic/283169-problem-with-my-code/#findComment-1455020 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.