Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 24th, 2005, 07:41 PM
Authorized User
 
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i have a home page that displays a user log in form. the user must enter a username and password. what i thought i might have to do is create a session that will hold the AccID in the session for the person with that username and password and somehow pass that AccID into the insert query on another form.

i tried some code but i am having trouble setting up the session and storing the AccID which is in mysql db into it and then referencing it in a different form.

I hope this helps i am lost when it comes to sessions and cookies.

Thanks in advanced

Scoobie

 
Old January 25th, 2005, 09:05 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

Here are some sample scripts.
Cookie setting

setcookie("user", trim($str_username), time()+365*24*60*60);

This sets a cookie of name = "user" and value = what is stored in variable $str_username

The expiry for the cookie is 1 year from now.

Cookie fetching

echo (isset($_COOKIE["user"]) ? $_COOKIE["user"] : "");

This will print the value of cookie of name "user" if such a cookie exists. Otherwise, nothing is printed.

Session setting

if(!(session_is_registered("gbl_username")))
{
    session_register("gbl_username");
}
$_SESSION["gbl_username"]=trim($str_username);

This will set a session variable of name "gbl_username" with value in variable $str_username

Fetching session value

if(session_is_registered("gbl_username"))
{
    echo $_SESSION["gbl_username"];
}

For using session in a page, you need to add

session_start();

at the top of the page before execution of any statement.

Setting session and cookie requires that you should not have sent anything to browser. i.e. no echo/print statements and any HTML should precede the session / cookie set command.
 
Old January 25th, 2005, 12:28 PM
Authorized User
 
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

would you be able to tell me which line goes where in the different php scripts below. Basically what i want is to insert the AccID of the person who logs in with a user name and pw on the first page and store this AccID in the ticket table against newly generated ticket num which happens in the 4th script. soo need a session.

Here is the code. i have been stuck on this problem for about two weeks.

HomePg.php
/* here the user enters their username and password and clicks submit which sends them to Login2.php */

<?php
session_start();
?>
<html><head>
<title>Members Only Login</title>
</head><body>
<?php
@ $db = mysql_pconnect("localhost");
if (!$db)
{
echo "Error: Could not connect to database.";
exit;
}
mysql_select_db("data");
?>
<table border = "0">
<tr>
<td colspan="3" bgcolor="gray" align="center">

<b>Members Only</b>

</td></tr>
<tr>
<td width="33%" align="top">

    <b>Already A Member?</b>


<form name="Login Form" method = "get" action = "Login2.php">
<table border="0">
<tr>
<td width="148">Email
</td>
<td>
<input type="text" name=UNameFrm size="30">
</td></tr>
<tr>
<td width = "148">Password
</td>
<td>
<input type="password" name=PW1 size="30">
</td></tr>
<tr>
<td align="center" colspan="2">
<br>
<input type = "submit" value = "Login">
</td></tr>
</table>
</td>
</form>
</td></tr>
</table>
</body></html>


Login2.php

/* Here the user just clicks on the purchase ticket button and this brings them to TimeData.php */

<?php
session_start();
?>
<html><head>
<title>Members Only Login</title>
</head><body>
<?php
if (!$UNameFrm || !$PW1){
print "<script type='text/javascript'>\n";
print "alert('You Must Enter Both Email and Password. Please Go Back and Try Again');\n";
print "</script>";
exit;
}
@ $db = mysql_pconnect("localhost");
if (!$db){
echo "Error: Could not connect to database.";
exit;
}
mysql_select_db("data");
$qry_FindMemDetails = "SELECT * FROM acctbl
WHERE acctbl.Username = '$UNameFrm' AND acctbl.PW = '$PW1'";
$FindMemDetails = mysql_query($qry_FindMemDetails);
$row_FindMemDetails = mysql_fetch_assoc($FindMemDetails);
$totalRows_FindMemDetails = mysql_num_rows($FindMemDetails);
?>
<center>
<table border = "0">
<tr>
<td colspan="3" bgcolor="gray" align="center">

<b>Welcome Back</b>

</td></tr>
<tr>
<td width="33%" align="top">

<b>Click On Buttons Below</b>

<br>
<table border="0">
<tr>
<td width="148">
<input type = "submit" name = TimeData value = "PurchaseTicket" onclick ="window.location.href='TimeData.php';">
</td></tr>
</table>
</td></tr>
</table>
</center>
</body></html>


TimeData.php

/* here the user justs selects how much time and clicks submit which calls Ticket.php */

<?php
session_start();
?>
<html>
<head>
<title>Purchase Time and Data</title>
</head>
<?php
@ $db = mysql_pconnect("localhost");
if (!$db)
{
echo "Error: Could not connect to database.";
exit;
}
mysql_select_db("demodata");
?>
<table border = "0">
<form name="Purchase Form" method="post" action="Ticket.php">
<tr>
<td colspan="3" bgcolor="gray" align="center">

<b>Purchase ticket</b>

</td>
</tr>
<tr>
<td>
<center>
<table>
<tr>
<td>
<input type="radio" name=TimeDataFrm value="Buy Time" checked>Buy Time
</td>
</tr>
<tr>
<td width = "178">
<select name=OptionFrm>
<?php
$qry = "SELECT TimeData, Cost, Description FROM Chrgtbl WHERE TimeData Like '%mins%'";
$result = mysql_query($qry);
while($row = mysql_fetch_assoc($result))
echo "<option value = '".$row['TimeData']."'>".$row['TimeData']." || € ".$row['Cost']." || ".$row['Description']."</option>";
?>
</select>
</td>
</tr>
</table>
</center>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type = "submit" name = PurchaseTicket value = "Purchase a Ticket">
</td>
</tr>
</form>
</table>
</body>
</html>


Ticket.php

/* when the user clicks submit button on TimeData.php this script generates a random number and inserts it into the database */

<?php
session_start();
?>
<html>
<head>
<title>Purchase Ticket</title>
</head>
<body>
<?php
@ $db = mysql_pconnect("localhost");
if (!$db)
{
echo "Error: Could not connect to database.";
exit;
}
mysql_select_db("data");
/* Random Function */
function RandomNumber($length)
{
global $Random;
$Random = srand((double)microtime()*1000000);
$data = "AbcDE123GHIJK4LMN567QRSTU89VWXYZ";
$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
$data .= "0FGH45OP89";
for($i = 0; $i < $length; $i++)
{
$Random .= substr($data, (rand()%(strlen($data))), 1);
}
echo "$Random <br>";
}
RandomNumber(8);
$TodaysDate = date("Y-m-d h:m:s");
echo "$TodaysDate <br>";

$qry = "select ChrgID from chrgtbl where chrgtbl.TimeData = '$OptionFrm'";
$result = mysql_query($qry) or die(mysql_error());
$row = mysql_fetch_object($result);

/* INSERT DETAILS INTO TICKET TABLE */
$insert = "insert into tickettbl (AccID, ChrgID, TicketNo, TimeData, RecDate)
        values ('".$_SESSION['AccID']."', '$row->ChrgID', '$Random', '$OptionFrm', '$TodaysDate')";
$result = mysql_query($insert) or die(mysql_error());
if ($result)
echo "Entered";
?>
</body>
</html>



 **********************************************


what i want to do is have a session that stores the username and password of the person when they login at the home page and use this info to do a select query in the ticket.php that will select the accid from the acctbl where the username equals that of the person logging in.

Please note that the Field name in the db are called Username & PW and the textboxes that the users types in to are called $UNameFrm & $PW1.

Can you please help me i am so lost.

thanks in advanced,

scoobie








Similar Threads
Thread Thread Starter Forum Replies Last Post
update table from another db table timbal25 SQL Server 2005 3 January 19th, 2008 06:47 AM
Update one table to another table using DTS in SQL Printmaker SQL Language 0 July 24th, 2007 07:17 AM
DTS return table ID forkhead SQL Server DTS 2 November 9th, 2006 12:15 PM
How to Update one table with other table data? ramk_1978 SQL Language 2 May 26th, 2006 12:51 AM
Update parent table with the sum of child table gbrown SQL Language 2 November 9th, 2004 07:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.