Jump to content

Session help


snowman15

Recommended Posts

I have an index.php page that I have a login form on.

I have another page (login.php) that checks to see if the username and password is valid from my database.

 

The login.php redirects them back to the index.php page. Instead of the login form, i want it to say something like hello <username>.

 

someone told me to use sessions but every time i try to use a session it doesnt display anything. I dotn know if the variable is beign carried over or if its even possible to be carried over to a different php page.

 

I have the start_session() thing on top of each of the pages. I define the $_SESSION['user'] variable on the login.php page but when i get redirected back, it doesnt have a value and its not showing up.

 

 

Any help with sessions or suggestions?

Link to comment
Share on other sites

I have the start_session() thing on top of each of the pages. I define the $_SESSION['user'] variable on the login.php page but when i get redirected back, it doesnt have a value and its not showing up.

 

By your description, it should work. Were going to need to see your login.php and index.php pages.

Link to comment
Share on other sites

this is my login.php

<?php session_start();//SESSION START AND DECLARING SESSIONS
$_SESSION['user']=0;
$_SESSION['userid']=0; ?>
<html>
<head>
<?php
$username=$_POST['user'];
$password=$_POST['password'];

$_SESSION['user']=$user; // SETTING THE SESSIONS
$_SESSION['userid']=$userid;
?>
<SCRIPT LANGUAGE="JavaScript"><!--
function redirect () { setTimeout("go_now()",1); }
function go_now ()   { window.location.href = "index2.php"; }
//--></SCRIPT>
</head>
<BODY onLoad="redirect()">
</body>
</html>

 

 

This is the index2.php page that it gets redirected to

<?php session_start(); ?>// STARTING SESSION? is this right?
<html>
<head>
<title>XXXXX</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: xx-large;
font-weight: bold;
color: #0000FF;
font-family: "DeathRattle BB";
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #0000CC;
}
a:hover {
text-decoration: underline;
color: #666666;
}
a:active {
text-decoration: none;
}
hr{
color:#333333;
}
.footer{
border-bottom:thin #000000 solid;
border-left:thin #000000 solid;
border-right:thin #000000 solid;
border-top:thin #000000 solid;
}
body{
margin:10px auto;
.style5 {font-family: "DeathRattle BB"}
-->
</style>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="800" height="700" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000" class="footer">
<tr>
	<td colspan="2"><table width="812" height="109" border="0" background="images/index_01.gif">
          <tr>
            <td width="806" height="105" align="center" valign="bottom"><table width="531" height="89" border="0">
              <tr>
                <td><div align="left"><span class="style1">XXXXX</span></div></td>
              </tr>
            </table>
            <div align="right"></div></td>
          </tr>
        </table></td>
</tr>
<tr>
	<td height="31" colspan="2"><table width="814" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
          <tr>
            <td width="107" align="center" background="images/index_02.gif"><a href="XXXXXX"><strong>Home</strong></a></td>
            <td width="110" align="center" background="images/index_02.gif"><a href="XXXX"><strong>Games</strong></a></td>
            <td width="597" height="31" align="center" background="images/index_02.gif"> </td>
          </tr>
        </table></td>
</tr>
<tr>
	<td width="202"><table width="202" height="560" border="0" cellpadding="0" cellspacing="0" background="images/index_03.gif">
          
          
          <tr>
            <td align="center" valign="top"><p>
		<form action="login.php" method="post">
              <p>Username:
                <input type="text" name="user" />
                Password:
                <input type="password" name="password" />
                <br />
                <input type="submit" value="Login"/>
              </p>

              </form>
		  <br>
		  <?php

		  echo "Hello ".$_SESSION['user']; // THIS IS WHERE ITS NOT PRINTING USER
		  
		  ?>
		  
		  
		  
		  <br>
		</td>

          </tr>
        </table></td>
	<td width="612" align="center" valign="top" background="images/index_04.gif"><p> </p>
  </td>
</tr>
</table>

<div style="font-size: 0.8em; text-align: center; margin-top: 1.0em; margin-bottom: 1.0em;">
<span class="style5">XXXXXXXXX</span>- <a href="XXXXXXXXXXXXXXXX</a></div>
</body>
</html>

 

 

i want it to print the name where it says hello <name>

its not working

Link to comment
Share on other sites

1) Get rid of that initialisation:

     $_SESSION['user']=0;
      $_SESSION['userid']=0; 

2) You're assigning the value of $_POST['user'] to a variable called '$username'

$username=$_POST['user'];
$password=$_POST['password'];

and later a variable called "$user" to 'user' (Member of the Session array):

$_SESSION['user']=$user; // SETTING THE SESSIONS

 

I reckon it should be :

$_SESSION['user']=$username; // SETTING THE SESSIONS

 

.. to start with..

 

And, why using blody javascript for a simple task such as redirecting??

Just do it with PHP, e.g like this:

<?php
header( 'Location: http://www.yoursite.com/blah.html' ) ;
exit;
?>

check this out: http://au2.php.net/header

 

..or use http refresh etc..

 

Cheers!

 

 

Link to comment
Share on other sites

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.