Good day!

I just to ask why require_once function display the content of the whole page in the second page?

I have two pages index.php and mem_accnt.php. I have session in index.php. Now i need to get the session in index.php and display it in mem_accnt.php. In order to get the session value, i need to put this code at the top of mem-accnt.php.

<?php require_once('index.php'); ?>

But why is it that the whole content of index.php will display in mem_accnt.php?

pls help..thank you!

Recommended Answers

All 10 Replies

What you have listed is going to call the entire index.php file, not just the session, which is why you see everything duplicated. If you are just looking for the session part, just put the following code at the top of the mem_accnt.php

<?PHP

session_start();

?>

That will pick up the session along with any variables you are passing with the session.

ive already put <?php session_start(); ?> at the top of mem_accnt.php but it returns empty value for the session. But when i put <?php require_once('index.php'); ?> the session value will display.. To make this clear, here is the assignment code in index.php for session.

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE user_name = %s and password=%s", GetSQLValueString($colname_rec_mem_accnt, "text"),GetSQLValueString($colpass_rec_mem_accnt, "text"));

$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);

if ($totalRows_rec_mem_accnt) {
	 $_SESSION['MM_UserID']= $row_rec_mem_accnt['member_id'];
}
?>

And heres the code in mem_accnt.php where i picked up the value for session.

<?php require_once('Connections/nmpc_web_conn.php'); ?>
<?php session_start(); ?>

$colname_rec_mem_accnt = "-1";
if (isset($_SESSION['MM_UserID'])) {
  $colname_rec_mem_accnt = $_SESSION['MM_UserID'];
}

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE member_id = %s", GetSQLValueString($colname_rec_mem_accnt, "text"));
$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);
?>

when i tried to echo the session, it will supposed to be display the session value for member id, but it returns null..

Whats wrong with the statement..

Thank you for helping.!

Member Avatar for TechySafi

Look in your first code,

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE user_name = %s and password=%s", GetSQLValueString($colname_rec_mem_accnt, "text"),GetSQLValueString($colpass_rec_mem_accnt, "text"));
 
$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);
 
if ($totalRows_rec_mem_accnt) {
	 $_SESSION['MM_UserID']= $row_rec_mem_accnt['member_id'];
}
?>

where is the session_start(); ?? write something like this

if ($totalRows_rec_mem_accnt) {
         session_start();
	 $_SESSION['MM_UserID']= $row_rec_mem_accnt['member_id'];
}

One more thing, you said you wrote the code of above on index.php but you didn't call index.php on mem_accnt.php ! Anyway I think if you write something like this, it should work fine

<?php
require_once('index.php');
echo $show_value=$_SESSION['MM_UserID'];
?>

Good luck :)

still the problem exist.

When i put require_once('index.php'); all index.php content will display and the value for session will display also..

If i will exclude require_once('index.php'); the session value is empty. whats wrong?

The code so far:

This code is in index.php

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE user_name = %s and password=%s", GetSQLValueString($colname_rec_mem_accnt, "text"),GetSQLValueString($colpass_rec_mem_accnt, "text"));

$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);

if ($totalRows_rec_mem_accnt) {
     session_start();
     session_register($_SESSION['MM_UserID']);

	 $_SESSION['MM_UserID']= $row_rec_mem_accnt['member_id'];
}

And here the code for mem_accnt.php where i want to get the session value.

<?php 
require_once('index.php');
session_start();

if (isset($_SESSION['MM_UserID'])) {
  
  $memid=$_SESSION['MM_UserID'];
}
 ?>

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE member_id = %s", GetSQLValueString($memid, "text"));
$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);
?>

echo $row_rec_mem_accnt['full_name'];

Thank you!

Member Avatar for TechySafi

session_register() This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

And yes when you include a script by include() then all of its codes are included. So post your full codes of index.php and mem_accnt.php so that I can find a way for you cuz I don't understand why you writing same codes in two different file and you are including one into another.

Post your code, and post what kinda warning or error you get.

and in mem_accnt.php I can see many mistakes, you writing php without declaring <?php or <? or somthing.... then you close the php tag and you are echoing outside of php. I dont understand, so I want to see your full code.

Thanks

just dont include the index.php to your mem_accnt.php file. once you have your session started in both pages... session can be access in all you files... just dont forget to put the session_start()

ok. I think we're missing something in here. This is the task to be done.. in index.php there is an option that the user will enter his/her user name & password. If the username & password will in the MySQL table which holds the login accounts, the mem_accnt.php will show and then i will echo the Full Name of the user who login..Now the event is that, my login form is associated with fancy box..Here is the code where i compare the login user name and password in mysql db followed by the fancy box integration..

<?php
$colname_rec_mem_accnt = "-1";
if (isset($_POST['#login_name'])) {
  $colname_rec_mem_accnt = $_POST['#login_name'];
}

$colpass_rec_mem_accnt = "-1";
if (isset($_POST['#login_pass'])) {
  $colpass_rec_mem_accnt = $_POST['#login_pass'];
}

mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE user_name = %s and password=%s", GetSQLValueString($colname_rec_mem_accnt, "text"),GetSQLValueString($colpass_rec_mem_accnt, "text"));

$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);

if ($totalRows_rec_mem_accnt) {
     session_start();
	 $_SESSION['MM_UserID']= $row_rec_mem_accnt['member_id'];
}
?>

this is now the form which the fancy box integrates.

<!-- MEMBERS LOGIN POPUP --> 
 
    <div class="style24" style="display:none">

	<form id="login_form" method="POST" action="">
    
      <p id="login_error">Enter your User Name & Password...</p>
        
		
			<label for="login_name">Enter User Name: </label>
			<br><input type="text" id="login_name" name="login_name" size="40" /></br>
	 
        
		<p>
			<label for="login_pass">Enter Password: </label>
			<br><input type="password" id="login_pass" name="login_pass" size="40" /></br>
	  </p>
        
		<p>
			<input type="submit" value="Login" />
	   </p>
        
		<p>
		    <em>( Chat Admin to ask for User Name & Password )</em>	  </p>
	</form>
</div>

and this is how the fancy box integrate the form by form id..And i think this is the area where i should assign the session to make it work..

<!-- MEMBERS LOGIN POPUP WITH FANCY BOX--> 
 
<script type="text/javascript">
$(document).ready(function() {
   $("#popup").fancybox({
    'scrolling'		: 'no',
    'titleShow'		: false,
    'transitionIn'      : 'elastic',
    'transitionOut'     : 'fade',
    'onClosed'		: function() {
	$("#login_error").hide();
	}
  });
 });
$("#login_form").bind("submit", function() {

	if ($("#login_name").val().length < 1 || $("#login_pass").val().length < 1) {
	    $("#login_error").show();
	    $.fancybox.resize();
	    return false;
	}

	$.fancybox.showActivity();

	$.ajax({
		type		: "POST",
		cache	        : false,
		url		: "members_accnt.php",
		data		: $(this).serializeArray(),
		success         : function(data) {
			         $.fancybox(data);
		}
	});

	return false;
});

 </script>

This fancy box works fine. Bu i think the a i forward the session to be available in mem_accnt.php is wrong..If there is a better solution rather than using session, pls revise my coe above.

This is the main event that happens in index.php. And i think this is the reason why my session wont work..I think i missed something in this fancy box..

If the login is corrent, i will echo the Full Name from mySQL tbl of the user who login in the mem_accnt.php..

This is how it works in mem_accnt.php:

<?php 

session_start();
 
if (isset($_SESSION['MM_UserID'])) {
 
  $memid=$_SESSION['MM_UserID'];
}
 ?>
 
mysql_select_db($database_nmpc_web_conn, $nmpc_web_conn);
$query_rec_mem_accnt = sprintf("SELECT * FROM tbl_members_list WHERE member_id = %s", GetSQLValueString($memid, "text"));
$rec_mem_accnt = mysql_query($query_rec_mem_accnt, $nmpc_web_conn) or die(mysql_error());
$row_rec_mem_accnt = mysql_fetch_assoc($rec_mem_accnt);
$totalRows_rec_mem_accnt = mysql_num_rows($rec_mem_accnt);
?>
 
echo $row_rec_mem_accnt['full_name'];

Thank you!

kindly try this: can you create a container where you can store the echoed full name: just like this

$.ajax({
		type		: "POST",
		cache	        : false,
		url		: "members_accnt.php",
		data		: $(this).serializeArray(),
		success         : function(data) {
			         $("#containerId").html(data)
		}
	});

and what i have observed in your mem_accnt.php file, there are two php closing tags... can you remove the first closing tag for php, the one above mysql_select_db


Im hoping it help you

by the way, sorry for my reply earlier...


i have to correct something in your code, when you click the submit button on your login form, it will initiate an ajax action from what i have observe, so the values of the username and password will be passed through members_account.php and it and will be used to query and verify if it exists, so the session for memId doesnt exist in the first place.... so what you should do is, the verifying of the login information should be in the members_account.php and the setting of the session once it is verified that it exists, you echo his username otherwise return an error message for it.... coz the problem really when i tried to look at your jquery ajax code, it really calls the members_account.php when you click the login button and doesnt execute the first code sample you have and there is no session set...

hope it will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.