Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP LOGIN Page Recommendation 1

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
ZA
Hi all, im bettling with creating a PHP login page, if i use re-direct from a login page all the session variables will be cleared.
Is there a better way to do this.

<code>
$_SESSION["myusername"] =$username;
$_SESSION["credit"] =$credit;
$_SESSION["companyid"] =$cid;
//echo "success";
header("Location: loginsuccess.php");

</code>

 
Hi

[ul]
[li]From that chunk there is no way to find out whether the session was started. Post all relevant pieces of code and settings.[/li]
[li]Note that an [tt]echo[/tt] before the [tt]header()[/tt] call is not a good idea.[/li]
[li]Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.[/li]
[/ul]


Feherke.
 
Thanks you so much, Here is how im doing it.

Code:
<?php session_start();?>
<?php 
//echo "Herbbbbbbbbbbbbbbbbbbbbbbbbbbe";
if (isset($_POST['push']))
{
				//$username = "testing"; //$_POST['UserName'];
					//$password = "testpass"; //$_POST['Password'];
					//echo $password;
					//echo $username;
					$username = $_POST['cmdpass'];
					$password = $_POST['cmduser'];
					$credit = "";
					$cid = "";
					include "dbConnect.php";
					$query= "SELECT clients.UserName,clients.UserName,callocation.CB,callocation.ExpireDate,callocation.ID FROM clients, callocation where clients.CID= callocation.ID and clients.username = '$username' AND clients.password = '$password'";
					//$query = "SELECT * FROM clients WHERE username = '$username' AND password = '$password'";			 
					$result = mysql_query($query) or die("Couldn't select infor");
					//$result=mysql_query($query);
					$num=mysql_numrows($result);
					//echo "recors found=".$num."<br>";
					mysql_close();
					if ($num > 0)
					{
						$i = 0;
						while ($i < $num)
						{
						   //echo "inwhile";
							$username = mysql_result($result, $i, "username");
							$credit = mysql_result($result, $i, "CB");
							$cid = mysql_result($result, $i, "ID");
							echo "<td><input type = \"hidden\" name = username id =username value = $username></td>";
							//echo "<br>";
							$i++;
						}
						$_SESSION["myusername"] =$username;
						$_SESSION["credit"] =$credit;
						$_SESSION["companyid"] =$cid;
						//echo "success";
						header("Location: bulkmpush.php?id=$username"); 
					}
					else
					{
							echo "   " . "<a href=index.php title=click here target=_blank>Try Again</a>";
							
							//header("location:index.html");
				
				return;						
						
					}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
.style15 {font-size: 10px}
.style16 {font-size: 12px}
.style17 {font-family: Georgia}
.style18 {font-family: "Book Antiqua"}
.style19 {color: #009999}
.style20 {color: #00FF99}
.style21 {color: #663300}
.style22 {font-family: "MS Reference Sans Serif"}
.style23 {font-family: "Comic Sans MS"}
.style24 {font-size: 11px}
.style3 {	font-size: 12px;
	color: #CCCCCC;
	font-weight: bold;
}
.style6 {	font-size: 14px;
	color: #003063;
	font-weight: bold;
}
    #login
    {
        width: 98px;
    }
</style>
</head>

<body>
<form action="index.php" method="post" enctype="multipart/form-data" >

<table width="100%" border="0" bgcolor="#FFFFFF">
  <tr>
    <td colspan="3" bgcolor="#FFFFFF"><img src="images/wirels_logo.gif" width="44" height="22" alt="wirels logo" /></td>
  </tr>
  <tr>
    <td colspan="3" align="left" bgcolor="#5286B5"><span class="style16"><em><strong>Wirels DRC WAP - [ver 1.0] </strong></em></span></td>
  </tr>
  <tr>
    <td colspan="3" bgcolor="#FFFFFF"><span class="style15"> <span class="style16"> <span class="style17"> <span class="style18"> <span class="style19"> <span class="style20"> <span class="style21"> <span class="style22"> <span class="style23"> <span class="style24">
      <marquee>
        <strong>Sigma Messaging</strong>
      </marquee>
    </span></span></span></span></span></span></span></span></span></span></td>
  </tr>
  <tr>
    <td bgcolor="#CCDDEE" class="style6"><a href="index.html">Advert</a></td>
    <td bgcolor="#CCDDEE" class="style6"><a href="index.html">Advert</a></td>
    <td width="734" bgcolor="#CCDDEE">&nbsp;</td>
  </tr>
  <tr>
    <td width="177" bgcolor="f7f7e7">Username</td>
    <td width="177" bgcolor="f7f7e7">     
      <input name="cmdpass" type="text" id="cmdpass" size="15" maxlength="15" /></td>
    <td rowspan="4" bgcolor="f7f7e7">&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="f7f7e7">Password</td>
    <td bgcolor="f7f7e7">     
      <input name="cmduser" type="text" id="cmduser" size="15" maxlength="15" /></td>
  </tr>
  <tr>
    <td bgcolor="f7f7e7">&nbsp;</td>
    <td bgcolor="f7f7e7">
        <input id="push" name = "push" type="submit" value="Login" /></td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="f7f7e7">&nbsp;</td>
  </tr>
  <tr>
    <td height="24" colspan="3" align="left" bgcolor="#5286B5"><span class="style3">Copy Rights . WirelsConnect 2011</span></td>
  </tr>  
</table>
</form>
</body>
</html>
 
Hi

I see no obvious problem there. But is quite difficult to follow. I suggest to clean it up abit and try this :
PHP:
[teal]<?php[/teal] [COLOR=darkgoldenrod]session_start[/color][teal]();[/teal] [teal]?>[/teal]
[teal]<?php[/teal]
[b]if[/b] [teal]([/teal][b]isset[/b][teal]([/teal][navy]$_POST[/navy][teal][[/teal][green][i]'push'[/i][/green][teal]]))[/teal] [teal]{[/teal]
  [navy]$username[/navy] [teal]=[/teal] [navy]$_POST[/navy][teal][[/teal][green][i]'cmdpass'[/i][/green][teal]];[/teal]
  [navy]$password[/navy] [teal]=[/teal] [navy]$_POST[/navy][teal][[/teal][green][i]'cmduser'[/i][/green][teal]];[/teal]
  [navy]$credit[/navy] [teal]=[/teal] [green][i]""[/i][/green][teal];[/teal]
  [navy]$cid[/navy] [teal]=[/teal] [green][i]""[/i][/green][teal];[/teal]
  [red]include[/red] [green][i]"dbConnect.php"[/i][/green][teal];[/teal]
  [navy]$query[/navy][teal]=[/teal] [green][i]"SELECT clients.UserName,clients.UserName,callocation.CB,callocation.ExpireDate,callocation.ID FROM clients, callocation where clients.CID=callocation.ID and clients.username = '%s' AND clients.password = '%s'"[/i][/green][teal];[/teal]
  [navy]$result[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][COLOR=darkgoldenrod]sprintf[/color][teal]([/teal][navy]$query[/navy][teal],[/teal] [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$username[/navy][teal]),[/teal] [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$password[/navy][teal]))[/teal] [b]or[/b] [b]die[/b][teal]([/teal][green][i]"Couldn't select infor"[/i][/green][teal]);[/teal]
  [b]if[/b] [teal]([/teal][navy]$row[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_fetch_assoc[/color][teal]([/teal][navy]$result[/navy][teal]))[/teal] [teal]{[/teal]
    [navy]$_SESSION[/navy][teal][[/teal][green][i]"myusername"[/i][/green][teal]][/teal] [teal]=[/teal] [navy]$username[/navy] [teal]=[/teal] [navy]$row[/navy][teal][[/teal][green][i]"username"[/i][/green][teal]);[/teal]
    [navy]$_SESSION[/navy][teal][[/teal][green][i]"credit"[/i][/green][teal]][/teal] [teal]=[/teal] [navy]$credit[/navy] [teal]=[/teal] [navy]$row[/navy][teal][[/teal][green][i]"CB"[/i][/green][teal]];[/teal]
    [navy]$_SESSION[/navy][teal][[/teal][green][i]"companyid"[/i][/green][teal]][/teal] [teal]=[/teal] [navy]$cid[/navy] [teal]=[/teal] [navy]$row[/navy][teal][[/teal][green][i]"ID"[/i][/green][teal]];[/teal]
    [COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]"Location: bulkmpush.php?id=$username"[/i][/green][teal]);[/teal]
    [b]exit[/b][teal];[/teal]
  [teal]}[/teal] [b]else[/b] [teal]{[/teal]
    [navy]$error_message_to_display_later[/navy][teal]=[/teal][green][i]'Try Again'[/i][/green][teal];[/teal]
  [teal]}[/teal]
[teal]}[/teal]
[teal]?>[/teal]
[red]<!DOCTYPE[/red] [maroon]html[/maroon] [maroon]PUBLIC[/maroon] [green][i]"-//W3C//DTD XHTML 1.0 Transitional//EN"[/i][/green] [green][i]"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL][/i][/green][red]>[/red]
[gray]<!-- ... the remaining HTML ... -->[/gray]
And add [tt][teal]<?php[/teal] echo [navy]$error_message_to_display_later[/navy][teal];[/teal] [teal]?>[/teal][/tt] somewhere around the login [tt]form[/tt] to inform the user if something gone wrong with his previous attempt.


Feherke.
 
Thanks so much, is this how i display the value of the seesion:
echo "here testing=".$_SESSION["myusername"];

I get the foolowing error message:

Notice: Undefined variable: _SESSION in C:\xampp\htdocs\DRCWAP\bulkmpush.php on line 3
here testing=
 
Hi

Do you have [tt][teal]<?php[/teal] session_start[teal]();[/teal] [teal]?>[/teal][/tt] in the bulkmpush.php too ?

Feherke.
 
Thank you so much, i didnt have that. I appreciate your assistsnce.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top