Page 1 of 1

session not initialising or destroying when initialised

Posted: Fri Jan 16, 2015 3:12 am
by shan2batman
hi,
im a php noob trying to echo session variable based on what the user name is but neither is the session initialising nor is it destroying as i can go from index to home page by typing it in the browser with out the user input can any one help me with this(pardon my english) my code is as follows(if im missing something please tutor me on that too as that wud b helpful becoz web resources on sessions r not helpful)
code in home.php:

Code: Select all

<?php
include '1.1.php';
include '1.php';
ob_start();
session_start();
$_SESSION['uname']=$s1||$b1;
 
 
?>
<!DOCTYPE html>
<html>
    <head>
  <title>fetch-array</title>
 
  
  
  <meta charset="UTF-8">
 
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
</script>  
  <link href="css/hello.css" rel="stylesheet">
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
    selector: "textarea#elm1",
    theme: "modern",
    width: 500,
    height: 150,
    plugins: [
  
         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
         "searchreplace wordcount visualblocks visualchars code media insertdatetime media nonbreaking",
         "save table contextmenu directionality emoticons template paste textcolor"
   ],
   content_css: "css/content.css",
   toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons", 
   style_formats: [
        {title: 'Bold text', inline: 'b'},
        {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
        {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
        {title: 'Example 1', inline: 'span', classes: 'example1'},
        {title: 'Example 2', inline: 'span', classes: 'example2'},
        {title: 'Table styles'},
        {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
    ]
 }); 
</script>
 
 
</head>
 
<body>
    <header> <div align="left"><a href="session_stop.php">logout</a></div></header>
 
 
<br>
 
<div id="container">
 
<div class="Container-left">
<?php echo $_SESSION['uname'];?>
</div>
    
    
note that it isn't also echoing any session variable too then here is my session stop page:

Code: Select all

<?php
session_start();
session_unset();
session_destroy();
if(session_destroy()==1){
setcookie('', NULL, time()-100,'/');
header('location:index.php');}
were am i going wrong??? :banghead: :crazy:

here is my 1.1php code:

Code: Select all

<?php
 
include 'config.php';
 
error_reporting(E_ALL ^ E_NOTICE);
   
    $b1= stripslashes($_POST['uname']);
	$b2= stripslashes($_POST['pass']);
$e1=mysqli_real_escape_string($conn,$b1);
$e2=mysqli_real_escape_string($conn,$b2);
	$sql2= "SELECT * FROM turfregister1 WHERE uname='$e1' and pass='$e2'";
 
$query2 = "SELECT id, uname FROM turfregister1 WHERE uname = '$e1'";
 
         if (isset($_POST['button1'])&& mysqli_query($conn,$sql2)) {
             session_start();
             header('location: home.php');
}
 else {
echo mysqli_error($conn);    
}
	
 
and 1.php code:

Code: Select all

<?php
 
include 'config.php';
include 'session_start.php';
error_reporting(E_ALL ^ E_NOTICE);
    $s1=stripslashes($_POST['uname']);
	$s2=stripslashes($_POST['pass']);
	$s3=stripslashes($_POST['no']);
	$s4=stripslashes($_POST['ei']);
	$s5=stripslashes($_POST['gender']);
	$s6=stripslashes($_POST['fname']);
	$s7=stripslashes($_POST['lname']);
        $a1=  mysqli_real_escape_string($conn,$s1);
	$a2=mysqli_real_escape_string($conn,$s2);
        $a3=mysqli_real_escape_string($conn,$s3);
        $a4=mysqli_real_escape_string($conn,$s4);
        $a5=mysqli_real_escape_string($conn,$s5);
        $a6=mysqli_real_escape_string($conn,$s6);
        $a7=mysqli_real_escape_string($conn,$s7);
	
	$sql="INSERT INTO turfregister1 (uname,pass,no,ei,gender,fname,lname) VALUES ('$a1', '$a2' ,$a3,'$a4','$a5','$a6','$a7') ";
	
	if (isset($_POST['submit']) && mysqli_query($conn,$sql)) {
            
            
            echo "data inserted";
            session_start();
		header ('location: home.php');
	}
	else {
		
echo mysqli_error($conn);		
		
	}
	
	
	
mysqli_close($conn);	
	

Re: session not initialising or destroying when initialised

Posted: Fri Jan 16, 2015 4:06 am
by requinix
Missing the HTML for the login form.

Are you sure you don't, maybe, see a "1" where you expect to see the username?

Re: session not initialising or destroying when initialised

Posted: Sat Jan 17, 2015 3:51 am
by shan2batman
actually that is just half the code here is my full code for the home.php page:

Code: Select all

<?php
session_start();
include 'config.php';
include '1.1.php';
include '1.php';
 $_SESSION['uname']= ($s1!='') ? $s1 : $b1;


?>
<!DOCTYPE html>
<html>
    <head>
  <title>fetch-array</title>

  
  
  <meta charset="UTF-8">

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
</script>  
  <link href="css/hello.css" rel="stylesheet">
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
    selector: "textarea#elm1",
    theme: "modern",
    width: 500,
    height: 150,
    plugins: [
  
         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
         "searchreplace wordcount visualblocks visualchars code media insertdatetime media nonbreaking",
         "save table contextmenu directionality emoticons template paste textcolor"
   ],
   content_css: "css/content.css",
   toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons", 
   style_formats: [
        {title: 'Bold text', inline: 'b'},
        {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
        {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
        {title: 'Example 1', inline: 'span', classes: 'example1'},
        {title: 'Example 2', inline: 'span', classes: 'example2'},
        {title: 'Table styles'},
        {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
    ]
 }); 
</script>


</head>

<body>
    <header> <div align="left"><a href="session_stop.php">logout</a> | <a href="session_start.php">session start</a></div> </header>


<br>

<div id="container">

<div class="Container-left">
<?php
if (isset($_SESSION['uname'])){
echo 'hello, '.$_SESSION['uname']. 'welcome to home page';
session_write_close();
}?>
</div>
    
    
    <div class="container-right"> <form action="1.2.php" method="post">
            <textarea id="elm1" height="190px" width="190px" name="text" placeholder="share wats on your mind"></textarea>
            <br>
            <input type="submit" name="button" value="Post" />
        </form> 
        <hr>
    <?php
include '1.2.php';
include 'config.php';

while ($post_fetch = mysqli_fetch_array($query1))
                {
    echo 'comment:'.$post_fetch['post'].'<br/>';
    echo '<div align="left"><small>date:'.$post_fetch['date'].'</div></small><br/><hr>';
    
}
    ?>
    </div>
</div>

<hr> <footer>© Copyright by shan</footer>
</body></html>
can u help me with this bro!

Re: session not initialising or destroying when initialised

Posted: Sat Jan 17, 2015 5:26 am
by requinix
The uname will only be set properly if the user just submitted the login form. That's not the case because you are redirecting to this page.

Your login code is where you should be setting the uname. Everywhere else simply reads that value.