Jump to content

Recommended Posts

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:

<?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:

<?php
session_start();
session_unset();
session_destroy();
if(session_destroy()==1){
setcookie('', NULL, time()-100,'/');
header('location:index.php');}

were am i going wrong???

here is my 1.1php code:


<?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:

<?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);	
	
	

your program logic, literally, is not correct, in this - 

$_SESSION['uname']=$s1||$b1;

that is setting the session variable to the logical (boolean) result of the value in $s1 OR'ed with the value in $b1.  this will be either a true or false value, depending on what values are in the two variables.

 

your registration processing and your login processing are two separate events. each one should be complete and distinct. if you want to automatically log the member in when registration is complete, just set the session variable in the registration code (note: you can set the username in a session variable, for display purposes, assuming you won't ever allow users to change their username, but you should set a session variable with the user_id, the auto-increment id from the user table for that user.)

 

you also need exit; statements after your header() redirect statements to prevent the rest of the code on your page from running while the browser is requesting the page given in the redirect statement.

 

lastly, forget about using php include statements to virtually copy/paste together your application. what you have shown is a wall of repetitive logic, form processing logic that's being ran unconditionally (which would be throwing a bunch of php errors), and logic that's trying to display query errors simply because no form was submitted. start with the simplest code that's need, until you gain experience that would let you write and troubleshoot more advanced code.

after a little research i did the following correction and it still isn't showing the user name here is the code for my home.php:

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

?>
<!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';

}?>
</div>

and the corrections to my session stop page are as follows:

<?php

session_unset();
$s_d=session_destroy();
if($s_d==1){
setcookie('', NULL, time()-100,'/');
header('location:index.php');}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.