my session starts is not working.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1611 Views - Last Post: 04 June 2009 - 12:57 AM Rate Topic: -----

#1 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

my session starts is not working.

Posted 02 June 2009 - 09:44 AM

Here is my home.php file

<?php session_start();

?>



<html>
<head>
<style type="text/css" media="all">
@import "css/stylemaster.css";
</style>
<title>Online Examination</title>
</head>

<body>
 <div id="page-container"> 
 		<div id="header"><h1>&nbsp;</h1>
   </div>
		<div id="main-nav">Main Nav</div>
   <div id="sidebar-a" >
   

 <font class="ws8" color="#FF9900" face="Tahoma"></font>
	<?php  echo "Welcome  $_SESSION[$username]"; ?>
			
		  </div>
		<div id="content">Content   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam gravida enim ut risus. 
	Praesent sapien purus, ultrices a, varius ac, suscipit ut, enim. Maecenas in lectus. 
	Donec in sapien in nibh rutrum gravida. Sed ut mauris. Fusce malesuada enim vitae lacus 
	euismod vulputate. Nullam rhoncus mauris ac metus. Maecenas vulputate aliquam odio. 
	Duis scelerisque justo a pede. Nam augue lorem, semper at, porta eget, placerat eget, 
	purus. Suspendisse mattis nunc vestibulum ligula. In hac habitasse platea dictumst.  <h2>About</h2>
	<p><strong>Enlighten Designs</strong> is an Internet solutions provider that specialises in 
	   front and back end development. To view some of the web sites we have created view our 
	   portfolio.</p>
	<p>We are currently undergoing a 'face lift', so if you have any questions or would 
	   like more information about the services we provide please feel free to contact us.</p>
	
	<h2>Contact Us</h2>
	<p>Phone:   (07) 853 6060<br />
	Fax:	 (07) 853 6060<br />
	Email:   <a href="mailto:info@enlighten.co.nz">info@enlighten.co.nz</a><br />
	P.O Box: 14159, Hamilton, New Zealand</p>
	<p><a href="#">More contact information...</a></p>

</div>
		<div id="footer"><div id="altnav">
		<a href="#">About</a> - 
		<a href="#">Services</a> - 
		<a href="#">Portfolio</a> - 
		<a href="#">Contact Us</a> - 
		<a href="#">Terms of Trade</a>
	</div>
	Copyright &copy; Enlighten Designs
	<br/>
	Powered by <a href="http://www.enlightenhosting.com/">Enlighten Hosting</a> and
	<a href="http://www.vadmin.co.nz/">Vadmin 3.0 CMS</a>
</div>
		
 </div>
 
</body>
</html>




HERE IS MY LOGIN.PHP


<?php

	require($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
	$connection= @mysql_connect($db_host,$db_user,$db_password) or die("Error Connecting");
	mysql_select_db($db_name,$connection);
   
	if (isset($_POST["username"]) && isset($_POST["password"]))
		{
			$username = mysql_real_escape_string($_POST["username"]);
			$password = mysql_real_escape_string($_POST["password"]);
		   
			$query = "SELECT * FROM login WHERE Username='$username' and Password='$password'";
		   
			echo ($query);
		   
		   
				$results = mysql_query($query);if(mysql_num_rows($results) == 1)
			{
				$_SESSION['username'] = $_REQUEST['username'];
				header("Location: home.php");
			}
		   
			else
			{
			   
				echo '<script type=""text/javascript""> alert("Access Denied! Incorrect Username or Password") </script>';			
			   
			}
		}
?>


<head>
<style type="text/css" media="all">
@import "css/stylemaster.css";.style1 {font-size: 10px}
.style3 {font-size: 10px; font-weight: bold; }
.style4 {color: #FFFFFF}
body {
	background-color: #FFFFFF;
}
</style>
</head>


<body>   

<div id="page-container"> 
 		<div id="header"><h1>&nbsp;</h1>
   </div>
		<div id="main-nav">Main Nav</div>
		
		<div id="sidebar-a">  
	  
<form id="form1" name="form1" method="post" action="home.php">
			
		  <h3 class="style1">&gt;&gt; Login </h3>
		  <pre>User Type :  <select name="usertype" size="1" id="usertype">					   <option selected="selected">Student</option>					   <option>Lectuerer</option>					   <option>Administrator</option>					   </select>				 
		  </pre>
		  <pre>User Name:   <input name="username" type="text" id="username" value="" size="12px" />		   
		  </pre>
		  <pre>Password :&nbsp;  <input type="password" name="password" id="password" size="12px"/>
		  </pre>
		  <pre>			 <input type="submit" name="Login" value="Submit" />		   
		  </pre>
<p>Not a member yet?<br>
			<a href="register.php">Register Now!</a></p>
		  <p>&nbsp; </p>
</form>
</div>

	 </body>


WHEN I LOGGED IN USING A USERNAME. I WANT THAT USERNAME TO SHOW IN MY HOME PAGE. PLS HELP ME SOMONE

AT THIS TIME THE ERROR IS

Notice: Undefined variable: username in C:\wamp\www\home.php on line 25

Is This A Good Question/Topic? 0
  • +

Replies To: my session starts is not working.

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: my session starts is not working.

Posted 02 June 2009 - 09:54 AM

Greetings

First off, $_SESSION[$username] should be $_SESSION['username'] (on your home page).
Also, on your login.php page, you need to have it start with session_start(); aswell, as you do with ANY page that reads/writes sessions.


Yours,
Shane~

This post has been edited by ShaneK: 02 June 2009 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: my session starts is not working.

Posted 02 June 2009 - 10:00 AM

This:
<?php  echo "Welcome  $_SESSION[$username]"; ?>
Should be:
<?php  echo "Welcome  $_SESSION['username']"; ?>

Yes, what Shanek, said. I took too long reading through the rest of your code!! I strongly suggest that you learn OOP. You don't have much there, but it's already getting cumbersome because you're not using it.

This post has been edited by CTphpnwb: 02 June 2009 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 02 June 2009 - 04:38 PM

ya i did that too. but then when i put ' ' inside that Session

i get this error

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\home.php on line 25
Was This Post Helpful? 0
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: my session starts is not working.

Posted 02 June 2009 - 05:06 PM

The problem you're facing is that you're jumping out of your echo code, because if you're using ' for the key of the "Session array" then you're using ' in the echo. Basically, you're saying:
<?php  echo 'Welcome  $_SESSION['username']'; ?>
which will not work. Try this:
echo "Welcome ".$_SESSION['username'];


Yours,
Shane~

This post has been edited by ShaneK: 02 June 2009 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: my session starts is not working.

Posted 02 June 2009 - 10:48 PM

try this:
if(!isset($_SESSION))
{
session_start();
}

Was This Post Helpful? 0
  • +
  • -

#7 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 05:08 PM

:S its still not working. i have no clue whats going on
Was This Post Helpful? 0
  • +
  • -

#8 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 09:02 PM

can someone help me with this. coz i stuck in here for last 4 days :(
Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 09:04 PM

Any errors output?

From everything we've told you, this should have worked with at least one of the solutions we've offered.

Yours,
Shane~
Was This Post Helpful? 1
  • +
  • -

#10 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 09:06 PM

View PostShaneK, on 3 Jun, 2009 - 08:04 PM, said:

Any errors output?

From everything we've told you, this should have worked with at least one of the solutions we've offered.

Yours,
Shane~



still it didnt got the username i guess. now im using the lab pc. having different error now :S dont know whats going on with this
Was This Post Helpful? 0
  • +
  • -

#11 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: my session starts is not working.

Posted 03 June 2009 - 09:18 PM

ok here is a simple code i created 3 pages index.php, Login.php, page2.php

index.php
<form name="form1" method="post" action="login.php">
  <label>
  <input type="text" name="txtsession" id="txtsession">
  </label>
  <label>
  <input type="submit" name="button" id="button" value="Submit">
  </label>
</form>


login.php
<?php
if(!isset($_SESSION))
{
session_start();
}
if(isset($_POST['txtsession']))
{
$_SESSION['Username']=$_POST['txtsession'];
header("Location: page2.php");
}
else
{
header("Location: index.php");
}
?>


and page2.php
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
<h1>Welcome To My Site Dear <?php echo $_SESSION['Username']?>


try this:
Was This Post Helpful? 0
  • +
  • -

#12 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 09:31 PM

thanks a lot. ill check now.
Was This Post Helpful? 0
  • +
  • -

#13 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 10:22 PM

excellent.. thanks all

its working fine.. but the thing is.. the username which they taking is the table username. the user in the database y is that

here is the code. pls help me with that. i want to check the fields if its correct that field username should pops up




<?php

   require($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
	$connection= @mysql_connect($db_host,$db_user,$db_password) or die("Error Connecting");
	mysql_select_db($db_name,$connection);
   
	if (isset($_POST["username"]) && isset($_POST["password"]))
		{
			$username = mysql_real_escape_string($_POST["username"]);
			$password = mysql_real_escape_string($_POST["password"]);
		   
			$query = "SELECT * FROM login WHERE Username='$username' and Password='$password'";
		   
			echo ($query);
		   
		   
				$results = mysql_query($query);if(mysql_num_rows($results) == 1)
			{
			}
			
			if(isset($_POST['username']))
	{
	$_SESSION['username']=$_POST['username'];
		header("Location: page2.php");
	}
	else
	{
		header("Location: home.php");

	}
}

		?>

<html>
<head>
<style type="text/css" media="all">
@import "css/stylemaster.css";.style1 {font-size: 10px}
.style3 {font-size: 10px; font-weight: bold; }
.style4 {color: #FFFFFF}
body {
	background-color: #FFFFFF;
}
</style>
</head>


<body>   

<div id="page-container"> 
 		<div id="header"><h1>&nbsp;</h1>
   </div>
		<div id="main-nav">Main Nav</div>
		
		<div id="sidebar-a">  
	  
<form id="form1" name="form1" method="post" action="home.php">
			
		  <h3 class="style1">&gt;&gt; Login </h3>
		  <pre>User Type :  <select name="usertype" size="1" id="usertype">					   <option selected="selected">Student</option>					   <option>Lectuerer</option>					   <option>Administrator</option>					   </select>				 
		  </pre>
		  <pre>User Name:   <input name="username" type="text" id="username" value="" size="12px" />		   
		  </pre>
		  <pre>Password :&nbsp;  <input type="password" name="password" id="password" size="12px"/>
		  </pre>
		  <pre>			 <input type="submit" name="Login" value="Submit" />		   
		  </pre>
<p>Not a member yet?<br>
			<a href="register.php">Register Now!</a></p>
		  <p>&nbsp; </p>
</form>
</div>

	 </body>
	 
	 </html>




crap.. :S :( i forgot its not working. its going to the other else thing. :S ill recheck it again and see. thanks guys for the marvelous support

btw can anyone explain how my database connection names pops up. y is that
Was This Post Helpful? 0
  • +
  • -

#14 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: my session starts is not working.

Posted 03 June 2009 - 10:37 PM

i solved your this problem in your previews topic.
try this i Debugged your code
<?php

   require($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
	$connection= @mysql_connect($db_host,$db_user,$db_password) or die("Error Connecting");
	mysql_select_db($db_name,$connection);

if (isset($_POST["username"]) && isset($_POST["password"]))
{
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);		   
$query = "SELECT * FROM login WHERE Username='$username' and Password='$password'";	
$results = mysql_query($query);if(mysql_num_rows($results) == 1)
$_SESSION['username']=$_POST['username'];
header("Location: page2.php");
}
else
{
header("Location: home.php");
}
?>

<html>
<head>
<style type="text/css" media="all">
@import "css/stylemaster.css";.style1 {font-size: 10px}
.style3 {font-size: 10px; font-weight: bold; }
.style4 {color: #FFFFFF}
body {
	background-color: #FFFFFF;
}
</style>
</head>


<body>  

<div id="page-container">
		 <div id="header"><h1> </h1>
   </div>
		<div id="main-nav">Main Nav</div>
	   
		<div id="sidebar-a">  
	 
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
		   
		  <h3 class="style1">&gt;&gt; Login </h3>
		  <pre>User Type :  <select name="usertype" size="1" id="usertype">					   <option selected="selected">Student</option>					   <option>Lectuerer</option>					   <option>Administrator</option>					   </select>				
		  </pre>
		  <pre>User Name:   <input name="username" type="text" id="username" value="" size="12px" />		  
		  </pre>
		  <pre>Password :   <input type="password" name="password" id="password" size="12px"/>
		  </pre>
		  <pre>			 <input type="submit" name="Login" value="Submit" />		  
		  </pre>
<p>Not a member yet?<br>
			<a href="register.php">Register Now!</a></p>
		  <p>  </p>
</form>
</div>

	 </body>
	 
	 </html>


This post has been edited by noorahmad: 03 June 2009 - 10:37 PM

Was This Post Helpful? 0
  • +
  • -

#15 kunkka  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 15-May 09

Re: my session starts is not working.

Posted 03 June 2009 - 11:49 PM

thanks a lot.

appreciate the support a lot...

Session is working fine. but the username should match the database login table username..

but it isnt.. y is that..
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2