Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,252 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,220 people online right now. Registration is fast and FREE... Join Now!




How using session?

2 Pages V  1 2 >  
Reply to this topicStart new topic

How using session?

chaminda7245
10 Oct, 2008 - 08:09 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 14

Hi,Please let me know how i can passing the username from one page to another page by using session.(Php,MySQL) blink.gif blink.gif blink.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How Using Session?
10 Oct, 2008 - 08:10 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How Using Session?
10 Oct, 2008 - 08:13 PM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,460



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
http://us.php.net/manual/en/ref.session.php

It's the ol' Teach a man to fish routine.

Best of luck, please feel free to post any specific questions.
User is online!Profile CardPM
+Quote Post

Moonbat
RE: How Using Session?
10 Oct, 2008 - 08:15 PM
Post #4

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
A user named joeyadms has a great tutorial on sessions here on DIC. Here's the link:

Session Handling in PHP by joeyadms
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How Using Session?
10 Oct, 2008 - 08:16 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,460



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Joey Adams has submitted some great stuff.
User is online!Profile CardPM
+Quote Post

chaminda7245
RE: How Using Session?
10 Oct, 2008 - 08:42 PM
Post #6

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 14

I have four pages.

01) main_login.php

CODE


<html>
<head>
<title>Members Login</title>
</head>

<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>


02)check_login.php

CODE


<?php
$host="localhost"; // Host name
$username="admin"; // Mysql username
$password="1234"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>


03)login_success.php
CODE


<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
<?php
echo "welcome".$_SESSION['myusername'];
?>
</body>
</html>


04)logout.php
CODE

// Put this code in first line of web page.
<?
session_start();
session_destroy();
?>


my problem is when user login success need show he's username in login-success.php

i use echo "welcome".$_SESSION['myusername'];.But user name is not display.Please help me


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How Using Session?
10 Oct, 2008 - 08:47 PM
Post #7

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,460



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
In check login...
CODE

$_SESSION['myusername']=$username;


Then in login_success.php...
CODE

echo "welcome".$_SESSION['myusername'];

User is online!Profile CardPM
+Quote Post

chaminda7245
RE: How Using Session?
10 Oct, 2008 - 09:00 PM
Post #8

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 14

QUOTE(no2pencil @ 10 Oct, 2008 - 09:47 PM) *

In check login...
CODE

$_SESSION['myusername']=$username;


Then in login_success.php...
CODE

echo "welcome".$_SESSION['myusername'];




I have change the codes.Still not display username
User is offlineProfile CardPM
+Quote Post

chaminda7245
RE: How Using Session?
10 Oct, 2008 - 09:12 PM
Post #9

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 14

I have change the codes and check.Still have a same problem.Please can you check it again.

User is offlineProfile CardPM
+Quote Post

chaminda7245
RE: How Using Session?
10 Oct, 2008 - 10:40 PM
Post #10

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 14

Please help me to find problem!
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: How Using Session?
11 Oct, 2008 - 08:32 AM
Post #11

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
What version of PHP are you using? You may have to use $HTTP_SESSION_VARS to access the session data instead of $SESSION
User is offlineProfile CardPM
+Quote Post

jstephens
RE: How Using Session?
14 Oct, 2008 - 05:56 AM
Post #12

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 191



Thanked: 1 times
My Contributions
try this just as a test. I know that you created a local var called myusername. But still that is how I would start my troubleshooting.

CODE

$_SESSION['myusername']=$_Post['username'];


This post has been edited by jstephens: 14 Oct, 2008 - 06:00 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:19AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month