Jump to content

Recommended Posts

login.php

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("shopping") or die(mysql_error()); 

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{ 
	$username = $_COOKIE['ID_my_site']; 
	$pass = $_COOKIE['Key_my_site'];
	 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
	while($info = mysql_fetch_array( $check )) 	
		{
		if ($pass != $info['password']) 
			{
			 			}
		else
			{
			header("Location: login.php");

			}
		}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
	if(!$_POST['username'] | !$_POST['pass']) {
		die('You did not fill in a required field.');
	}
	// checks it against the database

	if (!get_magic_quotes_gpc()) {
		$_POST['email'] = addslashes($_POST['email']);
	}
	$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
		die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
				}
while($info = mysql_fetch_array( $check )) 	
{
$_POST['pass'] = stripslashes($_POST['pass']);
	$info['password'] = stripslashes($info['password']);
	$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
	if ($_POST['pass'] != $info['password']) {
		die('Incorrect password, please try again.');
	}
else 
{ 
  // if login is ok then we add a cookie 
	 $_POST['username'] = stripslashes($_POST['username']); 
	 $hour = time() + 3600; 
setcookie(ID_my_site, $_POST['username'], $hour); 
setcookie(Key_my_site, $_POST['pass'], $hour);	 

//then redirect them to the members area 
header("Location: products.php"); 
} 
} 
} 
else 
{	 
  // if they are not logged in 
?> 
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2>Login</td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40"> 
</td></tr> 
<tr><td>Password:</td><td> 
<input type="password" name="pass" maxlength="40"> 
</td></tr> 
<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form> 
<?php 
} 
?> 

"registerpage" add.php

 <?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("shopping") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
		die('You did not complete all of the required fields');
	}

// checks if the username is in use
	if (!get_magic_quotes_gpc()) {
		$_POST['username'] = addslashes($_POST['username']);
	}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
		die('Sorry, the username '.$_POST['username'].' is already in use.');
				}

// this makes sure both passwords entered match
	if ($_POST['pass'] != $_POST['pass2']) {
		die('Your passwords did not match. ');
	}

	// here we encrypt the password and add slashes if needed
	$_POST['pass'] = md5($_POST['pass']);
	if (!get_magic_quotes_gpc()) {
		$_POST['pass'] = addslashes($_POST['pass']);
		$_POST['username'] = addslashes($_POST['username']);
			}

// now we insert it into the database
	$insert = "INSERT INTO users (username, password)
			VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
	$add_member = mysql_query($insert);
	?>

<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>

<?php 
} 
else 
{	
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>
<?php
}
?> 

products.php

 <?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("shopping") or die(mysql_error()); 

//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
	$username = $_COOKIE['ID_my_site']; 
	$pass = $_COOKIE['Key_my_site']; 
	 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
	while($info = mysql_fetch_array( $check )) 	 
		{ 

//if the cookie has the wrong password, they are taken to the login page 
		if ($pass != $info['password']) 
			{ 			header("Location: login.php"); 
			} 

//otherwise they are shown the admin area	 
	else 
			{ 
			 echo "Admin Area<p>"; 
echo "Your Content<p>"; 
echo "<a href=logout.php>Logout</a>"; 
			} 
		} 
		} 
else 

//if the cookie does not exist, they are taken to the login screen 
{			 
header("Location: login.php"); 
} 
?> 
<?php
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
	$pid=$_REQUEST['productid'];
	addtocart($pid,1);
	header("location:shoppingcart.php");
	exit();
}
?>
<!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>Products</title>
<script language="javascript">
function addtocart(pid){
	document.form1.productid.value=pid;
	document.form1.command.value='add';
	document.form1.submit();
}
</script>
</head>
<body>
<form name="form1">
<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>
<div align="center">
<h1 align="center">Products</h1>
<table border="0" cellpadding="2px" width="600px">
	<?php
		$result=mysql_query("SELECT * FROM products");
		while($row=mysql_fetch_array($result)){
	?>
    	<tr>
        	<td><img src="<?php echo $row['picture'] ?>" /></td>
            <td>   	<b><?php echo $row['name'] ?></b><br />
            		<?php echo $row['description'] ?><br />
                    Price:<big style="color:green">$
				<?php echo $row['price'] ?></big><br /><br />
               <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /> 
		</td>
	</tr>
        <tr><td colspan="2"><hr size="1" /></td>
        <?php
	 }
    ?>
    </table>
</div>
</body>
</html>

shoppingcart.php

<?php
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
	remove_product($_REQUEST['pid']);
}
else if($_REQUEST['command']=='clear'){
	unset($_SESSION['cart']);
}
else if($_REQUEST['command']=='update'){
	$max=count($_SESSION['cart']);
	for($i=0;$i<$max;$i++){
		$pid=$_SESSION['cart'][$i]['productid'];
		$q=intval($_REQUEST['product'.$pid]);
		if($q>0 && $q<=999){
			$_SESSION['cart'][$i]['qty']=$q;
		}
		else{
			$msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
		}
	}
}

?>
<!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>Shopping Cart</title>
<script language="javascript">
function del(pid){
	if(confirm('Do you really mean to delete this item')){
		document.form1.pid.value=pid;
		document.form1.command.value='delete';
		document.form1.submit();
	}
}
function clear_cart(){
	if(confirm('This will empty your shopping cart, continue?')){
		document.form1.command.value='clear';
		document.form1.submit();
	}
}
function update_cart(){
	document.form1.command.value='update';
	document.form1.submit();
}


</script>
</head>

<body>
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
    	<h1 align="center">Your Shopping Cart</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
    </div>
    	<div style="color:#F00"><?php echo $msg?></div>
    	<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
    	<?php
		if(is_array($_SESSION['cart'])){
            	echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
			$max=count($_SESSION['cart']);
			for($i=0;$i<$max;$i++){
				$pid=$_SESSION['cart'][$i]['productid'];
				$q=$_SESSION['cart'][$i]['qty'];
				$pname=get_product_name($pid);
				if($q==0) continue;
		?>
            		<tr bgcolor="#FFFFFF"><td><?php echo $i+1 ?></td><td><?php echo $pname?></td>
                    <td>$ <?php echo get_price($pid)?></td>
                    <td><input type="text" name="product<?php $pid?>" value="<?php echo $q ?>" maxlength="3" size="2" /></td>                    
                    <td>$ <?php echo get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?php echo $pid?>)">Remove</a></td></tr>
            <?php			
			}
		?>
			<tr><td><b>Order Total: $<?php echo get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr>
	<?php
            }
		else{
			echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
		}
	?>
        </table>
    </div>
</form>
</body>
</html>

lopgout.php

 <?php 
$past = time() - 100; 
//this makes the time in the past to destroy the cookie 
setcookie(ID_my_site, gone, $past); 
setcookie(Key_my_site, gone, $past);
?> 

<html>
<body>
<head>
<meta http-equiv='refresh' content='2;url=login.php>
Logout Successful
</head>
</body>
</html>

billing.php

<?php
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='update'){
	$name=$_REQUEST['name'];
	$email=$_REQUEST['email'];
	$address=$_REQUEST['address'];
	$phone=$_REQUEST['phone'];

	$result=mysql_query("insert into customers values('','$name','$email','$address','$phone')");
	$customerid=mysql_insert_id();
	$date=date('Y-m-d');
	$result=mysql_query("insert into orders values('','$date','$customerid')");
	$orderid=mysql_insert_id();

	$max=count($_SESSION['cart']);
	for($i=0;$i<$max;$i++){
		$pid=$_SESSION['cart'][$i]['productid'];
		$q=$_SESSION['cart'][$i]['qty'];
		$price=get_price($pid);
		mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)");
	}
	die('Thank You! your order has been placed!');
}
?>
<!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>Billing Info</title>
<script language="javascript">
function validate(){
	var f=document.form1;
	if(f.name.value==''){
		alert('Your name is required');
		f.name.focus();
		return false;
	}
	f.command.value='update';
	f.submit();
}
</script>
</head>


<body>
<form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
<div align="center">
        <h1 align="center">Billing Info</h1>
        <table border="0" cellpadding="2px">
        	<tr><td>Order Total:</td><td><?php get_order_total()?></td></tr>
            <tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
            <tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
            <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
            <tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
            <tr><td> </td><td><input type="submit" value="Place Order" /></td></tr>
        </table>
</div>
</form>
</body>
</html>

includes\db.php

<?php
@mysql_connect("localhost", "root", "") or die("Demo is not available, please try again later");
@mysql_select_db("shopping") or die("Demo is not available, please try again later");
session_start();
?>

includes\functions.php

<?php
function get_product_name($pid){
	$result=mysql_query("select name from products where serial=$pid");
	$row=mysql_fetch_array($result);
	return $row['name'];
}
function get_price($pid){
	$result=mysql_query("select price from products where serial=$pid");
	$row=mysql_fetch_array($result);
	return $row['price'];
}
function remove_product($pid){
	$pid=intval($pid);
	$max=count($_SESSION['cart']);
	for($i=0;$i<$max;$i++){
		if($pid==$_SESSION['cart'][$i]['productid']){
			unset($_SESSION['cart'][$i]);
			break;
		}
	}
	$_SESSION['cart']=array_values($_SESSION['cart']);
}
function get_order_total(){
	$max=count($_SESSION['cart']);
	$sum=0;
	for($i=0;$i<$max;$i++){
		$pid=$_SESSION['cart'][$i]['productid'];
		$q=$_SESSION['cart'][$i]['qty'];
		$price=get_price($pid);
		$sum+=$price*$q;
	}
	return $sum;
}
function addtocart($pid,$q){
	if($pid<1 or $q<1) return;

	if(is_array($_SESSION['cart'])){
		if(product_exists($pid)) return;
		$max=count($_SESSION['cart']);
		$_SESSION['cart'][$max]['productid']=$pid;
		$_SESSION['cart'][$max]['qty']=$q;
	}
	else{
		$_SESSION['cart']=array();
		$_SESSION['cart'][0]['productid']=$pid;
		$_SESSION['cart'][0]['qty']=$q;
	}
}
function product_exists($pid){
	$pid=intval($pid);
	$max=count($_SESSION['cart']);
	$flag=0;
	for($i=0;$i<$max;$i++){
		if($pid==$_SESSION['cart'][$i]['productid']){
			$flag=1;
			break;
		}
	}
	return $flag;
}

?>

 

My Problems are :

1- in poducts page this msg appears

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampplite\htdocs\ShoppingCart\products.php:1) in C:\xampplite\htdocs\ShoppingCart\includes\db.php  on line 4

 

2- in products page when i click at "add to card" it show this msg

Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\ShoppingCart\products.php:1) in C:\xampplite\htdocs\ShoppingCart\products.php on line 44
and it dont redirect me to shoppingcart page

 

3- logout page doesn't work  :wtf: it show this msg

Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\ShoppingCart\logout.php:1) in C:\xampplite\htdocs\ShoppingCart\logout.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\ShoppingCart\logout.php:1) in C:\xampplite\htdocs\ShoppingCart\logout.php on line 5

Logout Successful

and dont redirect me into login page it says
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

 

    *  This problem can sometimes be caused by disabling or refusing to accept

          cookies.

 

4- i think sessions dont work probaly

 

 

sooo helppppppp meeeeee plzzzzzz!!!!!!!!!!    :'(  :'(  :'(

Link to comment
https://forums.phpfreaks.com/topic/203856-help-me-to-experts-only/
Share on other sites

Your problem can be solved by "output buffering"

 

you can do 2 things to control this;

 

1) at the beginning of your php script add the function "ob_start();"

 

OR

 

2) Switch on output buffering in your php.ini file. Search for "output_buffering" current;y your one is probably set to "off"

 

Make the change and restart your server.

 

That should do the trick regarding "headers already sent"

 

 

 

 

Notice, your solution was in the first reply:

 

Read the sticky post (especially reply #2) at this link - http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

 

Quote from: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

The following information should help you resolve your "headers already sent" warning messages:

 

Check that there's no characters (output) before a starting PHP tag <?PHP

Check that there's no characters (output) after an ending PHP tag ?>

 

Next time you should read the sticky and try what it suggest to fix the error, as that sticky was set to help you solve your own issues. It just takes a small effort to read it.

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.