php sessions are not being saved -


i apologize wall of text i've been banging head against wall around problem awhile i'm gonna try provide information possible. i'm not quite sure if problem i'm getting has user sessions (i'm new php), that's seems me.

i ask user enter login information (id , password) enter system in ask_login.php:

<div class="login_box">         <h1>login</h1>         <form method="post" action="login.php">         <p><input type="text" name="username" placeholder="userid"></p>         <p><input type="password" name="password" placeholder="password"></p>         <input type="submit" name="submit" value="login"></p>         </form> </div> 

if login details (id , password) found in database user gets logged in user portal (login.php) can check details, exams dates, etc.. problem whenever login, if click example on details button check user details, redirects me ask_login.php page asking login details again saying didn't enter id/password details. i've tried removing code checks if login forms submitted blank, , started working , able click 'details' button or other button, without getting redirected ask_login.php. when click on 'details' button "welcome, username" line doesn't show username, makes me think has php sessions. furthermore, query make won't show result.

here's login.php code:

<?php session_start();   $username = $_post['username']; $password = $_post['password'];  if($username && $password) {   $conn_error = 'could not connect.';   $mysql_db = '------';   if(!mysql_connect('localhost', '------', '') || !mysql_select_db($mysql_db)) {   die($conn_error); }   $query = mysql_query("select * users id='$username' , password='$password'");      $numrows = mysql_num_rows($query);        if($numrows!== 0)        {           while($row = mysql_fetch_assoc($query))           {             $dbusername = $row['id'];             $dbpassword = $row['password'];           }            if($username==$dbusername && $password==$dbpassword) {             //echo "you logged in!";             @$_session['id'] = $username;           }           else {             echo "<script>alert('username/password incorrect');</script>";             echo "<script language='javascript'>window.location = 'ask_login.php';</script>";             die();             //die("wrong username/password!");           }       }         else {             echo "<script>alert('user doesn't exist.');</script>";             echo "<script language='javascript'>window.location = 'ask_login.php';</script>";             die();            //die("that user doesn't exist!");          } }      else if(empty($username) || empty($password)) {    echo "<script>alert('you didn't enter id/password');</script>";     echo "<script language='javascript'>window.location = 'ask_login.php';</script>";     die();     //die("please enter id , password!"); }  ?>    <!doctype html>  <html> <head>     <title>logged in | fcul</title>     <link rel="stylesheet" href="css/stylesheet_loggedin.css" type="text/css"/>     <meta http-equiv="content-type" content="text/html;charset=utf-8" />         <link rel="shortcut icon" href="img/vitor-20130904-favicon.ico"/> </head>   <body>              <div id="header">                 <br/>                     <a href="index.php"><img src="/img/fcul_cent_logo_001.png" width="510" height="70"/></a>             </div>   <div id="loggedinas"> <br/>  welcome,  <?php  $result = mysql_query("select nome users id='$username'"); while($row = mysql_fetch_assoc($result)) {     echo $row["nome"]; }  ?>   ( <?php echo $username;  ?> )   <br/>    <div id="logout">  <a href="logout.php"><font size="2"><u>[logout]</u></a></font></a> </div>  <hr/> </div>     <?php //fetch user's bi     if(isset($_post['username'] )) {      $id = $_request['username'];    $query = "select bi users id='$id'";        //if query successful     if($query_run = mysql_query($query)) {        //if returns 0 rows     if(mysql_num_rows($query_run)==null) {     echo "<script>alert('unexpected error 004');</script>";     echo "<script language='javascript'>window.location = 'index.php';</script>";     }        while($query_row = mysql_fetch_assoc($query_run)) {       $bi = $query_row['bi'];       //echo $bi;       }   }  }  ?>    <br/> <center> <div id="buttons">  <form method="post" action="login.php">    <input type="submit" name="details" value="details">     </form>  <?php //**print user's bi if clicks on 'details' button**     if($_post['detalhes']){     echo '<div id="content">' . $bi . '</div>'; } ?>   </div> </center> </body> </html> 

you cannot access session on first time insert in $_session['id'] = $username variable.
can access on second run of session_start();

try this.
1. make login.php
2. make welcome.php

try separate module login.php process checking login process if condition success

<? if($username==$dbusername && $password==$dbpassword) {     //echo "you logged in!";     $_session['id'] = $username;     header("location: welcome.php"); } ?> 

in welcome.php

<?  session_start(); // checking if user loged in if (!$_session['id']) {  header("location: ask_login.php");  exit; } ?> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -