FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need Help : Dreamweaver sessions





salman_500
hey guyz,

i wanted help with a couple of things..

you see i use dreamweaver to progrma my site.. i use php/mysql technology to vie database records on various pages of a site im working on...

1) firstly i wanted to know that is there a way by which i can show random records... not the same wach time.... a new one on each refresh...

2) and another thing is.. i have this members system and all that.. i use dreamweaver for sessions and all that... and i want to know how can i display text like this:

Welcome, User ( where user is the name of the logged in member, or guest if not logged in..)

3) also i want to know that if there is a way that i make the "Logout" link only appear when a user is logged in.. like it should not appear when the user is not logged in.. or is a guest.....

4) one last thing... i wana knwo if there is way that i can show error mesages over forms... like it happens on hotmail.com .. e.g here:





Any one who will tell me how to do it.. or can give me the code of it... i am ready to pay him/her with frih$'s....

as i seem to have 4 problems ill pay 25 frih$'s for the solution of each problem... if some1 can solve all of these problems for me ill throw in an extra 20 frih as bonus !!!!!
hexkid
salman_500 wrote:
1) firstly i wanted to know that is there a way by which i can show random records... not the same wach time.... a new one on each refresh...

Change your select to
Code:
select ... order by rand() limit 1



salman_500 wrote:
2) and another thing is.. i have this members system and all that.. i use dreamweaver for sessions and all that... and i want to know how can i display text like this:

Welcome, User ( where user is the name of the logged in member, or guest if not logged in..)

Code:
echo 'Welcome, <i>';
if (isset($_SESSION['username'])) {
  echo $_SESSION['username'];
} else {
  echo 'guest';
}
echo '</i>';



salman_500 wrote:
3) also i want to know that if there is a way that i make the "Logout" link only appear when a user is logged in.. like it should not appear when the user is not logged in.. or is a guest.....

Code:
if (isset($_SESSION['username'])) {
  echo '<a href="logout.php">Logout</a>';
}



Keep your FRIH$
salman_500
hey hexkid,

i really appreciate your effort in writig me all of this... but you see... none of the codes u gave me work...

as i said that i use dreamweaver to make pages.. my pages are html with php in them.. not pure php... like e.g...

for showing random records.... i want to be compatible with code like this :

Code:
........<td>Address : </td>
                              <td><?php echo $row_Recordset1['address']; ?></td>
                            </tr>
                            <tr>
                              <td>Society : </td>
                              <td><?php echo $row_Recordset1['society']; ?></td>
                            </tr>
................



and for the logout thing.. i have a code liek this:

Code:
<a href="<?php echo $logoutAction ?>">Log out</a>



in dreamweaver all of the php part of the page is in the top of the page... even before the tag
Code:
<HTML>
....

and maybe i have to add a function there and then add the activate code somewhere like the logout action...

plz i need help with this.... im still payin no matter wat !!!!!!!!!
hexkid
salman_500 wrote:
for showing random records.... i want to be compatible with code like this :

Code:
........<td>Address : </td>
                              <td><?php echo $row_Recordset1['address']; ?></td>
                            </tr>
                            <tr>
                              <td>Society : </td>
                              <td><?php echo $row_Recordset1['society']; ?></td>
                            </tr>
................

Somewhere before these lines you have something that sets $row_Recordset1, probably a mysql_fetch_array().
Before that mysql_fetch_array() you probably have a mysql_query() and it is the parameter to that mysql_query you have to change and add the "order by rand()" I wrote about before.


salman_500 wrote:
and for the logout thing.. i have a code liek this:

Code:
<a href="<?php echo $logoutAction ?>">Log out</a>

So just replace that with
Code:
<?php if (isset($_SESSION['username'])) { ?>
<a href="<?php echo $logoutAction ?>">Log out</a>
<?php } ?>
Of course I don't know what indexes you're using for your $_SESSION array, so my code is just an example you have to correct to the particulars of your code.


salman_500 wrote:
in dreamweaver all of the php part of the page is in the top of the page... even before the tag
Code:
<HTML>
....

and maybe i have to add a function there and then add the activate code somewhere like the logout action...

plz i need help with this.... im still payin no matter wat !!!!!!!!!
If you had posted your code I wouldn't have had to assume stuff ...
salman_500
ok... heres the code....

for logout thing.... here it is...... how it appears above he HTML tag.:

Code:
<?php
//initialize the session
session_start();

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  session_unregister('MM_Username');
  session_unregister('MM_UserGroup');
   
  $logoutGoTo = "login.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>


ion this one can you also tell me the code... i tried the one you provided me this time... but i had already tried thatr... but doesnt work.... so maybe this code will help....



here it is for retriving data from the data base for the records:

this is the whole code.. i pull in data to the page from 2 diferent tables one named as "housesale" the other as "houserent":

and i want both of them to show random records:

Code:
<?php
mysql_select_db($database_conn, $conn);
$query_Recordset1 = "SELECT * FROM housesale";
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_conn, $conn);
$query_Recordset2 = "SELECT * FROM houserent";
$Recordset2 = mysql_query($query_Recordset2, $conn) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>



Thnx !!!

im still going to pay !!!!!!!!!!!11
hexkid
salman_500 wrote:
ok... heres the code....

for logout thing.... here it is...... how it appears above he HTML tag.:

Code:
<?php
// ...

This should work (I'm not sure about session_unregister(), the manual says you shouldn't use it) when called from a URL such as example.com/logout.php?doLogout=true&a=b&c=d&e=f


salman_500 wrote:
here it is for retriving data from the data base for the records:

this is the whole code.. i pull in data to the page from 2 diferent tables one named as "housesale" the other as "houserent":

and i want both of them to show random records:


Code:
<?php
mysql_select_db($database_conn, $conn);

### hexkid changed the next line
$query_Recordset1 = "SELECT * FROM housesale ORDER BY RAND() LIMIT 1";
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_conn, $conn);

### hexkid changed the next line
$query_Recordset2 = "SELECT * FROM houserent ORDER BY RAND() LIMIT 1";
$Recordset2 = mysql_query($query_Recordset2, $conn) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>
salman_500
oh KOOOL !!!! Cool

the code for the random one... it actually works !!!!!

Thnx !!!!! *Frih$'s Sent*

but can you plz tell me how to display the username like i asked before.... and guest if not logged in !!!

plz !!!!

and i dont understand what your sayin and what should work... i did not rite the code.... it is generated my Dreamweaver itself.....

Tnx in advance ! Very Happy
SlowWalkere
From the code you pasted above, I wouldn't know the exact variables to use, but you'd want to do something like this...

Switch to Design View and put the cursor after where it says "Welcome ". In order to print either the username or guest after "Welcome," you need to insert a code block of php in the html and echo the variable that holds the username. To insert the code block, go to the Insert Menu -> PHP -> Code Block (or, switch to code view, and just type in <?php ?> and put the PHP between that).

I'm guessing that the username variable is unset until a user actually logs in, so if the username variable is unset then the user is a guest. In that case, use this code (replacing $username with your username variable).

Code:

<P>
Welcome
<?php
    if (!isset($username))
        echo "guest";
    else
        echo "$username";
?>
.
</P>


If your username variable is $_SESSION['MM_Username'] then use that instead of $username.

If you want to only show the "Logout" link when a person is logged in, you'd have to use a similar bit of code. Use an if statement to check if the username is set (and the user is logged in), and if so echo the link.

Code:

<?php
    if (isset($username))
        echo "<a href=\"logout.php\">Logout</a>";
?>


Just put in the URL that the Logout link usually points to, and you'll be set. Remember that when echo-ing a tag like that, you have to use a backslash before any quotes, or else the php won't convert to HTML correctly.

- Walkere[/code]
salman_500
hey,

first of all THNX !!! Very Happy Very Happy Very Happy Very Happy

wel... i still am facing problems....

you see in the welcome part.... i aded this code :

Code:
<p>
Welcome
<i><?php
    if (!isset($_SESSION['MM_Username']))
        echo "Guest";
    else
        echo "$username";
?> </i>
</p>


about this.. i know the first part works fine.. that when you are not logged in it show "Guest" and about the other part.. i know that when you are logged in... it does not show "Guest" but it does not show the username either.... you se i tried all of these in place of $username

"$_SESSION['MM_Username']"

"$MM_Username"

and other combinations.. but i cant get the username there... instead if i just write text.. like i tried with "bas"... i see

Welcom bas

but nothing when i enter seesion variables and stuff....

about the other part of showing log out only when logged in... well that code you gave me actually works.... and about what you said that bacslashes and all that..... i have somthinlike this does logs the person out :

"<a href="<?php echo $logoutAction ?>">Log out</a>"

and when i enter the backslaes this is what i see in the adress par of the explorer

"http://localhost/Real%20Estate/<?php%20echo%20/Real%20Estate/index.php?doLogout=true%20?>"

where as should be "http://localhost/Real%20Estate/index.php?doLogout=true"

and the page shows this:

Forbidden
You don't have permission to access /Real Estate/< on this server.


--------------------------------------------------------------------------------

Apache/2.0.53 (Win32) PHP/4.3.10 Server at localhost Port 80

NOTE : IM ON LOCAL HOST>>> MEANING MY OWN PC IS MY SERVER FOR TESTING ETC.....

although i have kinda discovered another way to make this work... but i still wana know if there is a way to make this possibly happen......

coz i have all my pages programmed for this...

SlowWalkere if you can make me do this... ill pay you 50 Frih$'s ...

and also if you can help me with the last problem that i have... the one for which i have provided a picture in my first post......plz do... i really need help with this !!!!

THNX !!!! in advance that is...... Very Happy
SlowWalkere
For the welcome thing, did you change the $username in the "else" part of the if statement? Because in the code you copied below, you checked if $_SESSION['username'] was set, and that same variable checked in the if statement should be the one echoed in the "else" part.

If that's not the right variable, then try checking in the part of the code that handles the log in function. Once the user logs in, some variable should be assigned the username taken from the form. Whatever that variable is should be used in the "if" statement and later echoed in the "else" part. If you can't find the variable, post the log-in section of the code here and maybe one of us can figure it out.

For the second part, I'm not quite sure what's going on there.

If that's the line that you put into the echo statement in the if statement, then the problem might be that you have another <?php ?> tag inside a php code block.

You'd want the echo statement to look like this...
echo "<a href=\"$logoutAction\">Log out</a>";

Those backslashes ensure that when the code is converted to HTML, the quotes remain (and the <a> tag works correctly). $logoutAction should be converted to its string value, giving you a link targeted at the URL contained in $logoutAction (which, from what you said, seems that it should be set to http://localhost/Real%20Estate/index.php?doLogout=true).

For the last question, I've got an idea of how to do it but I'm not sure if I could explain it correctly. But we'll try =)

In general, what you need to do is go to the script that handles logging in and add an error checking function. So, if you wanted to give the user a message that they entered an incorrect address/password combo, you would go to the location in the log-in script that checks if the password is correct for the given e-mail address. If it's not, send the user back to the log-in page and set a GET variable, say ErrorValue, equal to 1. (so url = ../login.php?ErrorValue=1 or some such thing). I'm not exactly sure how to do the re-direct, because everytime I try I get some error messages. But I think you need to user the Header function - Header("Location: http://login.php?ErrorValue=1"), where login.php is the location of the login page.

Now, in the actual log-in page, you need to enter a bit of code that checks if any error values are set. Find the HTML before where the Username field is displayed, and enter a code block like this...

Code:

<?php
    if (isset($_GET['ErrorValue']))
        switch $_GET['ErrorValue']:
        {
            default:
                break;

            case "1":
                echo "The e-mail address or password is incorrect.  Blah blah blah<BR>";
                 break;
        }
?>


What that does is check if ErrorValue is set, and then echo a certain statement based on the value of ErrorValue. If ErrorValue is set to "1", the script will echo the given error message. With only one error message, the switch statement doesn't make much sense, but it can be used to add extra error messages (where ErrorValue is set to something other than 1.

Hope that all made sense. And don't worry about the Fri$, I'm happy to help.

- Walkere
salman_500
Hey,

First of all Thnx for the logout part...it works splendidly !!!!

*Frih$'s Sent* (I keep my word) Very Happy

ok about the other 2 parts.....

the part where i have to show the username... like i said before the "if" part of the code i posted above works fine... even the other mart works fine... but i dunno what the session variable is.... it was set by dreamweaver... here is the login validate code... i took it off the login page..

maybe by looking at it you can see what the variable is...

i tried these in the else part

> "$Username"

> "$MM_Username"

> "$MM_UserName"

> "$username"

> "$UserName"

and all other ways of these... but cant get it to show the name....

Code:
<?php
// *** Validate request to login to this site.
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
  $GLOBALS['PrevUrl'] = $accesscheck;
  session_register('PrevUrl');
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "messages/loginsuccess.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_conn, $conn);
 
  $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username='%s' AND password='%s'",
    get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
   
  $LoginRS = mysql_query($LoginRS__query, $conn) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
   
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;        

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>


i always gave this a thought that maybe no name is assigned... but then i took a look at a page that was members only and its code was this:

Code:
<?php
session_start();
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  // For security, start by assuming the visitor is NOT authorized.
  $isValid = False;

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  if (!empty($UserName)) {
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
    // Parse the strings into arrays.
    $arrUsers = Explode(",", $strUsers);
    $arrGroups = Explode(",", $strGroups);
    if (in_array($UserName, $arrUsers)) {
      $isValid = true;
    }
    // Or, you may restrict access to only certain users based on their username.
    if (in_array($UserGroup, $arrGroups)) {
      $isValid = true;
    }
    if (($strUsers == "") && true) {
      $isValid = true;
    }
  }
  return $isValid;
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo);
  exit;
}
?>


meaning at this point ....
Code:
  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  if (!empty($UserName)) {


we know that a session variable is set...... but i cant figure anything else out...lol Confused Shocked

well for the other part..... that error message part..... is that the only code i have to enter ?? or is there somthin else that either i cant get or you missed out... coz when i enter that piece of code before the username field... in the HTML code i get an error message in te browser....

the code i entered above for the login thing... maybe we have to enter somthing there to give the error message...

Thnx for the ffort your putting in... but i really need help on this...

THnx !!
salman_500
-bump-
mathiaus
Have you got register globals on or off?

Try changing the first piece of code below to the second (found in your first piece of code)
Code:
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;       

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");

Code:
    // add session vars
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;


You then should be able to use
Code:
if (!isset($_SESSION['MM_Username']))
        echo "Guest";
    else
        echo $_SESSION['MM_Username'];


If not paste the below into your code and look for what your username is stored as
var_dump($_SESSION);
salman_500
Koool !!! it works as good as it can !! Very Happy

THNX !!!

*Frih's Sent* Very Happy

can you also plz check if you can do the last probem too?? the error one??

THNX again !!!


mathiaus wrote:
Have you got register globals on or off?

Try changing the first piece of code below to the second (found in your first piece of code)
Code:
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;       

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");

Code:
    // add session vars
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;


You then should be able to use
Code:
if (!isset($_SESSION['MM_Username']))
        echo "Guest";
    else
        echo $_SESSION['MM_Username'];


If not paste the below into your code and look for what your username is stored as
var_dump($_SESSION);
mathiaus
The error messages? I do things my own and I find it hard to explain so I'll give you a ittle example....
Code:
<?php

if($_POST['submit']) {
   // CHECK FORM INPUT
   // blah blah blah
   
   if( username and password are right ) {
      // LOGIN
   } else {
      // ERROR
      $errormess .= "The combination of username and password provided, are incorrect <br />";
   }

   if($errormess != '') {
      echo "<span style=\"color: red;\">".$errormess."</span>";
      // HTML FORM
   } else {
      // OTHER OUTPUT
   }
   
} else {
   // HTML FORM
}

?>


It depends what your doing really and what else is on the page
salman_500
i dont understand this... where do i put the code...

i have provided the login code above.... this is the form that logs in

Code:
<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
                          <div align="center"><span class="style12"><br>
                            Username :</span><br>
                              <input name="username" type="text" id="username2">
                              <br>
                              <br>
                              <span class="style12">Password :</span> <br>
                              <input name="password" type="password" id="password">
                              <br>
                              <br>
                              <input name="login" type="submit" id="login" value="Log in">
                          </div>
                      </form>
Related topics
php sessions
Dreamweaver Help!
PHP: Sessions and Cookies
[RESOLVED] Help with webpage
Help on Dreamweaver MX linking to frihost
Need some help with FTP
Looking for something to help build a horizontal menu
HELP ME BONDINGS!!!!
Dreamweaver help - not wygiwys
I have this problem with dreamweaver 8... can u help?
Help with Dreamweaver sessions !!!!!!!!
Im using apahe,php, and mysql and dreamweaver (HELP)
Phpbb Sessions - Need help getting guests/members.....
Need help on creating Div Tags in dreamweaver
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.