[SOLVED] Session Code (Were to place it)

This is a discussion on "[SOLVED] Session Code (Were to place it)" within the PHP Forum section. This forum, and the thread "[SOLVED] Session Code (Were to place it) are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 31st, 2008, 10:59
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Session Code (Were to place it)

When starting a session from user login, do i have to put the session code located below in every page??? For instance if the user logs in & is directed to a profile page\ then the user decided to say "view some site content on another page" would i have to put the code below in both pages to keep the session going from page to page, or is the process automatic..

It appears am still a bit confused with handling a php session
PHP: Select all

<?php session_start(); ?>if (isset($_SESSION['views']))                   
    $_SESSION['views']=$_SESSION['views']+1;
else
    $_SESSION['view']=1;
echo "views=". $_SESSION['views'];
?>
Thanks
Reply With Quote

  #2 (permalink)  
Old Jan 31st, 2008, 11:11
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Yes, you need to start the session on each page that you want access to previously set session variables.

This is unless you have set the php.ini directive session_auto_start.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Jan 31st, 2008, 11:17
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Thanks Mate! I thought it had to go on each page needed!

Can you just confirm my code is correct for doing this, and i will be on my way!

PHP: Select all

<?php
session_start
();if(isset($_SESSION['views']))
  
$_SESSION['views']=$_SESSION['views']+1;
else
  
$_SESSION['views']=1;
echo 
"Views="$_SESSION['views']; 
?>
Reply With Quote
  #4 (permalink)  
Old Jan 31st, 2008, 11:22
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

This is fine but it will only track the views and only show to the currently browsing user. It won't store the amount of profile views for everyone to see.

In order to do that, you would have to store the amount of views of each profile in the database.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Jan 31st, 2008, 11:33
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

I use the session code below in my edit profile page & the logged in users data loads into the page with no problems.

PHP: Select all

if (!isset($_SESSION)) {
 
session_start();
}

if (isset(
$_SESSION['MM_Username'])) {
 
$username $_SESSION['MM_Username'];

My question is: When the user who visits my website logs in, they are directod automatically to there profile page, but there data does not display in the page even after adding the code above. The closest i have got is it only displays the first registerd user in the database in the page, no matter which password & user name is input at the login page. Have you any ideas why this happens???

Also is the session code above the right code to use in my profile page to help tackle this problem of mine??

Thanks mate
Reply With Quote
  #6 (permalink)  
Old Jan 31st, 2008, 11:37
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

That's a completely different question

What does the URL look like when you are viewing someone's profile page?
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #7 (permalink)  
Old Jan 31st, 2008, 11:42
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Different question (Well u carnt blame me for using your big brain while your here)

Here the url dude one the user has logged in & transfered to the profile page: http://localhost/Content%20managment/Accounts.php

The profile doesnt display!!!
Reply With Quote
  #8 (permalink)  
Old Jan 31st, 2008, 11:44
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

NO, I mean what does the URL look like if you were looking at someone else's profile? If you need to direct a user the their profile page you simply need to send them to the URL for their profile.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #9 (permalink)  
Old Jan 31st, 2008, 11:52
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

I used dreamweavers in built functionality (So when i appled the login user to the form, i also used the option in the wizard to direct user to the profilepage once the user is logged in)! After this i attempted to use session code mentioned in the last post at the top of the page, then used record sets & bindings & were-function to display the logged in users profile data from the database!!!

I have posted the whole of the page code below:

PHP: Select all

<?php require_once('Connections/dbman.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}
$colname_rsRegist "-1";
if (isset(
$_GET['id'])) {
  
$colname_rsRegist $_GET['id'];
}
 
 
mysql_select_db($database_dbman$dbman);
$query_rsRegist sprintf("SELECT tbl_age.agename, tbl_developer.developername, tbl_regist.registname, tbl_regist.registusername, tbl_regist.registpassword, tbl_regist.registphoto FROM tbl_regist, tbl_age, tbl_developer WHERE registid = %s AND tbl_regist.registage = tbl_age.ageid AND tbl_regist.registdeveloper = tbl_developer.developerid"GetSQLValueString($colname_rsRegist"int"));
$rsRegist mysql_query($query_rsRegist$dbman) or die(mysql_error());
$row_rsRegist mysql_fetch_assoc($rsRegist);
$totalRows_rsRegist mysql_num_rows($rsRegist);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Profile</title>
<style type="text/css">
<!--
body {
 background-color: #F4F4FF;
}
-->
</style>
<link href="style/screen.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {
 color: #666666
}
-->
</style>
</head>
<body>
<div align="center">
  <p>Profile Display</p>
  <table width="597" height="527" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="597" height="34"><div align="center">Welcome. You are now logged in as: <?php echo $row_rsRegist['registname']; ?></div></td>
    </tr>
    <tr>
      <td height="459" valign="top"><table width="596" height="461" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="442" valign="top"><div align="center">
            <p>&nbsp;</p>
            <table width="295" height="249" border="0" cellpadding="0" cellspacing="0" id="frm_insert">
              <tr>
                <td width="121" height="47"><div align="left">Name:</div></td>
                <td width="174"><?php echo $row_rsRegist['registname']; ?></td>
              </tr>
              <tr>
                <td height="53"><div align="left">Age:</div></td>
                <td><?php echo $row_rsRegist['agename']; ?></td>
              </tr>
              <tr>
                <td><div align="left">Developemnt Skills:</div></td>
                <td><?php echo $row_rsRegist['developername']; ?></td>
              </tr>
              <tr>
                <td><div align="left">Login name:</div></td>
                <td><?php echo $row_rsRegist['registusername']; ?></td>
              </tr>
              <tr>
                <td><div align="left">Login Password:</div></td>
                <td><?php echo $row_rsRegist['registpassword']; ?></td>
              </tr>
            </table>
            <p>&nbsp;</p>
          </div></td>
          <td width="154" valign="top"><table width="154" height="461" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td height="196"><img src="uploads/<?php echo $row_rsRegist['registphoto']; ?> " width="197" height="228" alt="" /></td>
            </tr>
            <tr>
              <td valign="top"><table width="198" height="264" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td><div align="center"><a href="Index.php" class="style1">Index</a></div></td>
                </tr>
                <tr>
                  <td><div align="center"><a href="Edit_account.php" class="style1">Edit Profile</a></div></td>
                </tr>
                <tr>
                  <td><div align="center"><a href="Control_panel.php" class="style1">Control Panel</a></div></td>
                </tr>
                <tr>
                  <td><div align="center"><a href="Search_engine.php" class="style1">Search Members</a></div></td>
                </tr>
              </table></td>
            </tr>
          </table></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
    </tr>
  </table>
  <p>&nbsp;</p>
</div>
</body>
</html>
<?php
mysql_free_result
($rsRegist);
?>
Thanks for the help mate.
Reply With Quote
  #10 (permalink)  
Old Jan 31st, 2008, 11:55
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

But that doesn't answer my question

When you are browsing your site, say when you do a search for users and click on a user link to view their profile, what does the URL look like? That's all I want to know for now.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #11 (permalink)  
Old Jan 31st, 2008, 12:05
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Sorry feller!!

Here is the code from the view user profile button :
PHP: Select all

<td width="145"><div align="center"><a href="Accounts.php?id=<?php echo $row_rsResult['registid']; ?>">View</a></div></td>
Here is whats showen in the browser once the profile loads: http://localhost/Content%20managment/Accounts.php?id=2

Here is what is in the parrameter:
PHP: Select all

Accounts.php?id=<?php echo $row_rsResult['registid']; ?>

This part of my site works when i do a search & click the link to view the profile (The profile displays with no problems-belonging to that user) the problem i have is the profile data does not load automatically in the profile page once the user has logged in...

Hope this helps
Reply With Quote
  #12 (permalink)  
Old Jan 31st, 2008, 12:15
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

So it's just a matter of sending the user to that page when they log in...

Say this is their log in script

Code: Select all
if ($user && password)
{
 // username and password are all good
//retrieve the details from the database

// now after retreiving from the database, you have their user id

header('Location: htttp://localhost/Content%20management/Accounts.php?id=' . $userid);
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #13 (permalink)  
Old Jan 31st, 2008, 12:21
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Cool!!

So the code below needs to go into my login page - Not my Accounts.php page??

PHP: Select all

if ($user && password)
{
 
// username and password are all good
//retrieve the details from the database

// now after retreiving from the database, you have their user id

header('Location: htttp://localhost/Content%20management/Accounts.php?id=' $userid); 

And thats it, the logged in user data will display in the Accounts.php page??
Reply With Quote
  #14 (permalink)  
Old Jan 31st, 2008, 12:24
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

That's a blueprint it's not valid code.. It's a concept.

Bascically, when the user is logging in you will have access to the user's id. WIth that, you can just send the user to accounts page and append their id so it goes to their page.

It's the general idea.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #15 (permalink)  
Old Jan 31st, 2008, 12:33
Reputable Member
Join Date: Oct 2007
Location: Liverpool UK
Age: 29
Posts: 216
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Session Code (Were to place it)

Thanks Rakuli!!! I get your drift but fear i dont completly have the skill to manage it right now!!!

If i post my login script could you input a quick example to get me going...

Also do i still need to put the session code in the Accounts.php profile page!!

This is my last code problem and my site is good to go, i used the content managment site to test features out before implimenting them into my proper site...

Once i see somthink once i no how its done, but always half kills me getting there...

Here is my login script:

PHP: Select all

<?php require_once('Connections/dbman.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  
session_start();
}
$loginFormAction $_SERVER['PHP_SELF'];
if (isset(
$_GET['accesscheck'])) {
  
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
if (isset(
$_POST['username'])) {
  
$loginUsername=$_POST['username'];
  
$password=$_POST['password'];
  
$MM_fldUserAuthorization "";
  
$MM_redirectLoginSuccess "Accounts.php";
  
$MM_redirectLoginFailed "Login_failed.php";
  
$MM_redirecttoReferrer false;
  
mysql_select_db($database_dbman$dbman);
  
  
$LoginRS__query=sprintf("SELECT registusername, registpassword FROM tbl_regist WHERE registusername=%s AND registpassword=%s",
    
GetSQLValueString($loginUsername"text"), GetSQLValueString($password"text")); 
   
  
$LoginRS mysql_query($LoginRS__query$dbman) or die(mysql_error());
  
$loginFoundUser mysql_num_rows($LoginRS);
  if (
$loginFoundUser) {
     
$loginStrGroup "";
    
    
//declare two session variables and assign them
    
$_SESSION['MM_Username'] = $loginUsername;
    
$_SESSION['MM_UserGroup'] = $loginStrGroup;       
    if (isset(
$_SESSION['PrevUrl']) && false) {
      
$MM_redirectLoginSuccess $_SESSION['PrevUrl']; 
    }
    
header("Location: " $MM_redirectLoginSuccess );
  }
  else {
    
header("Location: "$MM_redirectLoginFailed );
  }
}
 
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
 background-color: #F4F4FF;
}
-->
</style>
<link href="style/screen.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {
 font-size: 12pt;
 color: #666666;
}
.style2 {font-size: 10pt}
-->
</style>
</head>
<body>
<div align="center">
  <p>Members Login</p>
  <table width="255" height="130" border="3" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
    <tr>
      <td valign="top"><form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction?>">
        <table width="250" height="126" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="144"><div align="right">Username:
              </div></td>
            <td width="106"><input type="text" name="username" id="username" /></td>
          </tr>
          <tr>
            <td><div align="right">Password:</div></td>
            <td><input type="text" name="password" id="password" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><div align="center">
              <input type="submit" name="button" id="button" value="Submit" />
            </div></td>
          </tr>
        </table>
            </form>
      </td>
    </tr>
  </table>
  <p>&nbsp;</p>
</div>
</body>
</html>
Cheers dude
Reply With Quote
  #16 (permalink)  
Old Jan 31st, 2008, 12:40
Rakuli's Avatar