PHP - Why Wont The Session Write Function Run?
Similar TutorialsNeed help ASAP. i need the following form to right to mysql table "table1" and columns "name" "email" and "comment".
<form action="sign.php" method="post" class="pure-form pure-form-stacked"> <fieldset> <label for="name">Your Name</label> <input id="name" type="text" placeholder="Your Name" name="name"> <label for="email">Your Email</label> <input id="email" type="email" placeholder="Your Email" name="email"> <label for="comment">Your Comments</label> <input id="comment" type="text" placeholder="Your Comments" name="comment"> <a href="thanks.html" button type="submit" class="pure-button">SUBMIT</button> </a> </fieldset> </form> My PHP File looks like this: <html> <body> <?php $myUser = "dbuser"; $myPassd = "********"; $myDB = "dbname"; $myserver = "192.168.1.80:3306"; $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; // Create connection $dbhandle = mysql_connect($myserver,$myUser,$myPassd) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; //select a database to work with $selected = mysql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //if (mysqli_connect_errno()) { //echo "Failed to connect to MySQL: " . mysqli_connect_error(); //} //echo "connected"; $sql = "INSERT INTO signatures (name, comment) VALUES ( $name, $comment)"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "record added"; mysql_close($dbhandle); ?> </body> </html> I also need this to redirect to another html page after it run. Like i said any help would be great because i cant get it to work! Not sure where to post this question. I have a MySQL database and add records with a PHP form to the tables. I have 2 fields (char) in one table. When the fields contain the character ' it wont write the record to the table! For example...if I enter. " John's house" it wont accept the record since ' appears in John's name! How do I work around this? Thanks I am trying to create an index page which contains registration and login field the problem that i get is on successful login a warning is displayed session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Eventz.com\index.php:116) in C:\xampp\htdocs\Eventz.com\index.php on line 235 This is the login part of my index.php this tag is inside an html table below the login form I also have a registration form and its php code above the login form Code: [Select] <?php if (isset($_REQUEST['pass'])) { $id=$_POST['id']; $pass=$_POST['pass']; $conn =mysql_connect("localhost","root",""); if (!$conn) { die('Could not connect: ' . mysql_error()); } /* checking connection....success! */ $e=mysql_select_db('test', $conn); if(!$e) { die(''.mysql_error()); } else { echo 'database selected successfully'; } if (isset($_REQUEST['id']) || (isset($_REQUEST['pass']))) { if($_REQUEST['id'] == "" || $_REQUEST['pass']=="") { echo "login fields cannot be empty"; } else { $sql=mysql_query("Select email,password from login where email='$id' AND password='$pass'"); $count=mysql_num_rows($sql); if($count==1) /* $count checks if username and password are in same row */ { session_start(); $_SESSION['id']=$id; echo "</br>Login Successful</br>"; } else { echo "</br>invalid</br>"; echo "please try to login again</br>"; } } } } ?> Any help or suggestion would be appreciated I am having trouble resolving an error. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/s519970/public_html/header.php:27) in /home/s519970/public_html/admin/login.php on line 2 What I can gather is I can't use "header (Location: 'admin.php')" after i've used session_start(). I have tried to replace the header (Location: 'admin.php') with this: echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; I've been trying to read up on solutions but haven't been able to get it sorted. If anyone can offer some advice that would be greatly appreciated as im new to php. Code: [Select] <?php session_start(); if(isset($_SESSION['user'])) echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; ?> <div id="loginform"> <form action="dologin.php" method="post"> <table> <tr> <td><span>Username:</span></td> <td><input type="text" name="username" /></td> </tr> <tr> <td><span>Password:</span></td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td> </tr> </table> </form> </div> I have tried using require_once('yourpage.php'); before my <head></head> tags in the header document where I've specified the html information but this doesn't seem to work. I've been advised to use ob_start("ob_gzhandler"); but I am not sure how to implement this. Any advice is greatly appreciated! in this page http://maximaart.com/newscp/ i have this problem Code: [Select] Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/maximasy/public_html/newscp/index.php:1) in /home/maximasy/public_html/newscp/index.php on line 2 my source code is <?php session_start(); include_once("config.php"); include_once("functions.php"); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === "$user" && $_POST['txtPassword'] === "$pass") { // the user id and password match, $_SESSION['basic_is_logged_in'] = true; require("main.php"); exit;?> I am pretty new to php and trying to teach myself. I can't get the values from this form to write to my flat file called orders.txt: browse_index.php <?php include("includes/menu_members.php") ?> <div id="content"> <h1>SHOPPING CART</h1> <a href="browse_index.php">CLICK HERE TO CONTINUE SHOPPING</a> <?php echo ' <table border="0"> <tr> <td><form id="f2" method="post"name="f2"><input type="submit" action="order_summary.php" name="submit2" value="submit order"></td> '; if(isset($_POST['submit'])) { $itemname = $_POST['h1']; //echo $_SESSION['itemname'][$itemname]; unset($_SESSION['itemqty'][$itemname]); unset($_SESSION['itemprice'][$itemname]); unset($_SESSION['itemname'][$itemname]); } echo "<br/><br/>"; echo "<table border='8' bgcolor='#efefef'>"; echo "<tr><th>Name</th><th>Quantity</th><th>Price</th><th>Subtotal</th></tr>"; foreach($_SESSION['itemname'] as $key=>$value) { echo '<tr><td><b>'.$_SESSION['itemname'][$key].'</b></td> <td>'.$_SESSION['itemqty'][$key].'</td> <td>$'.$_SESSION['itemprice'][$key].'</td> <td name="subtotal">$'.($_SESSION['itemqty'][$key] * $_SESSION['itemprice'][$key]).'</td> <td><form id="f1" method="post" name="f1"><input type="submit" name="submit" value = "delete"><input type="hidden" name="h1" value='.$key.'></td></tr>' ; } ?> order_summary.php: <?php session_start (); $date = date ("H:i jS F"); $outputstring = $date."/t" .$_POST['h1']. ":" .$_SESSION['itemqty'][$key]. ":" .$_SESSION['subtotal'][$key]. ":" ."\n"; $fp = fopen("orders.txt","a"); fwrite($fp, $outputstring); fclose($fp); ?> Can someone direct me where I am going wrong??? hi guys. i am trying this code: <?php count('Comedy'); ?> which is supposed to call this function: function count($cat){ $dbc = mysqli_connect('localhost', '60517', '59725972', '60517') or die ('Could connect to the sql db'); if(isset($cat)) { $query = "SELECT * FROM `movies` WHERE genre='$cat'"; } if(!isset($cat)) { $query = "SELECT * FROM movies"; } $result = mysqli_query($dbc, $query) or die('something went wrong with the query'); $movies = mysqli_num_rows($result); mysqli_close($dbc) ; echo $movies however nothing would be echoed. what am i doing wrong? i literally cant figure out whats going wrong in 6 lines of code. i am new to php so any help would be appreciated. sorry for bothering you. Well PHP manual says that the script execution will be delayed by $x seconds if you write a codes like this: Code: [Select] $x = 5; //5 secs sleep($x); Now I have this script in which I write the four lines to the screen one by one, each should be delayed by 5 secs after the last line is output to the screen. It does not work out at all... Code: [Select] echo "Update setting 1:"; sleep(5); echo "Success! <br>Updating setting 2"; sleep(5); echo "Success! <br>Updating setting 3"; sleep(5); echo "Success! <br>Updating setting 4"; Instead, the entire script execution is delayed by 15 secs, with all four lines output at the same time. Why is this happening? Is there a way to fix it? Please help... Hello: I have this snippet of code that will write the current year onto a page if it's embedded on the page itself: Code: [Select] <?php echo date("Y") ?> Works fine. But, when I include it in an included file and pull it onto the page with a function, it does not work. Like this: MyNav.php Code: [Select] <?php function spFooter() { $spFooter = " <p> © <?php echo date(\"Y\") ?> </p> "; return $spFooter; } ?> MyPage.php Code: [Select] <?php include('include/myNav.php'); ?> <html> ... <?php echo spFooter(); ?> ... </html> What is the proper way to do this, and also what are the basic rules for properly nesting PHP scripts like this? Thanks for the help. I'm trying to include a routine to catch an error but it doesn't seem to write to a text file within a function. just using the following simple code, even if I simply try to write a letter it doesn't work
if ($http_status == 200) { return $response; } else { $myfile = fopen("ErrorData.txt", "a+") or die("Unable to open file!"); $txt = date("H:i:sa d-m-Y")." / Error data- / ".json_encode($response)."\r\n"; fwrite($myfile, $txt); fclose($myfile); }
Edited September 13, 2019 by Dvae56 Added code Hi, I have some strange issues with my code: Code: [Select] <?PHP session_start(); $loginid = $_SESSION["valid_id"]; // First check if we are guest or user. if (!$_SESSION["valid_email"]) { $visitor = "yes"; } else { $visitor = "no"; $email = $_SESSION['valid_email']; $userid = $_SESSION['valid_id']; } //Load Header (blue menu) require("./inc/header.php"); //Load Sub-acc (silver account menu) require("./inc/sub-group.php"); //Load nav-group (Tabs) require("./inc/nav-group.php"); //Load Config file require("./inc/config.php"); //Set & get profile ID $getid = $_GET["id"]; //check ID $result = mysql_query("SELECT * FROM profiles WHERE id=('$getid') LIMIT 1"); $row = mysql_fetch_array($result); IF (mysql_num_rows($result) != 1) { exit("Invalid ID"); } //If we are guest, do we allow anon access to the profile IF ($row["privacy"] <= 10 && $visitor = "yes") { exit("You may not view this profile as a visitor, due to the users privacy settings"); } //Let's check if we are friends ELSEIF ($visitor = "no") { $result2 = mysql_query("SELECT * FROM profiles_friends WHERE user=('$getid') AND target=('$loginid') LIMIT 1"); $row2 = mysql_fetch_array($result2); $friends = $row2["status"]; if (mysql_num_rows($result2) = 0) { $friends = "no"; } } //If we are friend, do we allow access to the profile IF ($row["privacy"] >= 9 && $friends != 1) { exit("You may not view this profile because of the privacy settings."); } $row = mysql_fetch_array($result); $memgroup = $row["group"]; IF ($row["activated"] != 1) { exit("This account is suspended and cannot be viewed."); } //Check what group member is in. $result2 = mysql_query("SELECT * FROM profiles_groups WHERE id=('$memgroup') LIMIT 1"); $row2 = mysql_fetch_array($result); ?> Alright, so the error: Fatal error: Can't use function return value in write context in C:\xampp\htdocs\prog\profile.php on line 45 Code: [Select] 42. $result2 = mysql_query("SELECT * FROM profiles_friends WHERE user=('$getid') AND target=('$loginid') LIMIT 1"); 43. $row2 = mysql_fetch_array($result2); 44. $friends = $row2["status"]; 45. if (mysql_num_rows($result2) = 0) Alright, this is one thing that bothers me, the other is: Code: [Select] //check ID $result = mysql_query("SELECT * FROM profiles WHERE id=('$getid') LIMIT 1"); $row = mysql_fetch_array($result); IF (mysql_num_rows($result) != 1) { exit("Invalid ID"); I tried to put an invalid ID, and already here the script should have died/exited before executing the parts of the code that doesn't work. I tested my code on another page and it works flawlessly, perhaps this error is just generated before it actually exists i dunno.. Any way, any help is much appreciated } This is my error: Code: [Select] Fatal error: Can't use function return value in write context in /home/a8152576/public_html/SecretSanta.html on line 41 confused about why. This is the line 41: if(isset($_POST('submit')){ this is the php of my page (without the mysql data): <?php class functions{ public static function chooseGiftee($SS){ $done = false; while($done=false){ $sqllink= mysql_connect($mysql_host,$mysql_user,$mysql_password); if(!$sqllink){ echo "Could not connect! Please Try Again"; mysql_close($sqllink); } else { $selectGiftee = "SELECT * FROM SecretSantas ORDER BY RAND() LIMIT 0,10;"; while($row=mysql_fetch_array($result)){ $results = mysql_query($selectGiftee); if(!$row['Gifter']=''){ $done=false; } else { $gifteeResults = $row['Name']; $addSS = "INSERT INTO SecretSantas (Gifter) VALUES ('" .$SS. "')"; $done=true; return $gifteeResults; } } } mysql_close($sqllink); } } } if(isset($_POST('submit')){ $echoName = functions::chooseGiftee($_POST['gifterName']); echo $echoName; } ?> This is html: <form id="form1" name="form1" method="post" action=""> <p> <label>Your Name: <input type="text" name="gifterName" id="gifterName" /> </label> <input type="submit" name="submit" id="submit" value="Submit" /> </p> <p> </p> </form> Confused about the issue, first time using functions. Thanks in advance, Brandon I'm reading over this book and I came across something that looked very odd:
public function DisplayHeader() { ?> <table width="100%" cellpadding="12" cellspacing="0" border="0"> <tr bgcolor ="black"> <td align ="left"><img src = "logo.gif" /></td> <td> <h1>TLA Consulting Pty Ltd</h1> </td> <td align ="right"><img src = "logo.gif" /></td> </tr> </table> <?php }In the function it closes a php tag, uses raw html that is rendered as is on the page, and then returns back to opening the php tag. I understand why it is being done. It is easier to write raw html here than to echo it in a php block, but it just looks odd. Anyone actually use this technique? Hi, I could really do with some help putting the prices from two drop down boxes into sessions To start with the user can choose two seats one from each drop down box //DROP DOWN BOX ONE $extract =mysql_query("SELECT * FROM ride WHERE rideID ='1'"); $numrows = mysql_num_rows($extract); //start of form echo '<form method="post" name="f1" action="card.php">'; echo 'Please choose a seat: '; echo"<select name='seat' >"; while( $row = mysql_fetch_assoc($extract)) { $rideID = $row ['rideID']; $name = $row ['name']; $seatNumber = $row ['seatNumber']; $time = $row ['time']; $price = $row ['price']; echo "<option name='seat'> $seatNumber </option>"; $_SESSION['extract']=$_POST['seat']; } echo '</select><br>'; //DROP DOWN BOX TWO $extract2 =mysql_query("SELECT * FROM ride WHERE rideID ='1'"); $numrows = mysql_num_rows($extract2); //start of form echo '<form method="post" name="f2" action="card.php">'; echo 'Please choose a seat: '; echo"<select name='seat2' >"; while( $row = mysql_fetch_assoc($extract2)) { $rideID = $row ['rideID']; $rideName = $row ['rideName']; $seatNumber = $row ['seatNumber']; $time = $row ['time']; $price = $row ['price']; echo "<option name='seat2'> $seatNumber </option>"; $_SESSION['extract2']=$_POST['seat2']; } echo '</select><br>'; at the moment I have this : $query =mysql_query("SELECT price FROM ride WHERE rideID ='1' LIMIT 1"); while ($row=mysql_fetch_array($query)) { echo $row['price']; echo "<br>"; $seatPrice1=($row['price']); $seatPrice2=($row['price']*2); $vat1=17.5; function vat($vat, $row['price']) { return sprintf("%01.2f", ($vat*$row['price]/ 100)); } $vatprice = vat(17.5, 2.50); $total1=($vatprice); $total2=($vatprice*2); $endPrice1= ($total1+$seatPrice1); $endPrice2= ($total2+$seatPrice2); What I need is $endPrice1 and $endPrice2 in $_SESSIONS so i an INSERT them in to the database 3 pages away from this query/VAT fucntion. Can anyone help? Hello, I am puzzled as to how to get this code actually working, I am using some example code, I want the code to remove the data of the logged in users, at the moment it will remove all users registered in the mysql db. <?php # Access session. session_start() ; if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; } # Connect to the database. require ('connect_db.php'); // Attempt delete query execution $sql = "DELETE FROM users WHERE user_id=user_id"; if(mysqli_query($dbc, $sql, $q)){ echo "Records were deleted successfully."; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($dbc); } # Close database connection. mysqli_close( $dbc ) ; ?> $_session id knows that the user is logged into the control page, but for the code to remove the particular user logged in from the db, what exactly does this need? Thank you. Deprecated: Function session_register() is deprecated in /home/james/public_html/funshizzle.com/install/session.php on line 0 That is my error, I have no idea, What is it? i need help trying to get this delete feature to work its not deleting from the database (by the way i took out my database names and passwords at the top of the file) is it possible someone could help me, ive been working on this for like a week and cant figure out the problem. thanks! you can email me at spr_spng@yahoo.com picture 2.png is showing what it looks like Code: [Select] <?php $host="localhost"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="database_name"; // Database name $tbl_name="table_name"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <style> /*table affects look of the whole table look */ table { margin-left: auto; margin-right: auto; border: 1px solid #330000; border-collapse:collapse; width:70%; border-width: 5px 5px 5px 5px; border-spacing: 1px; border-style: outset outset outset outset; border-color: #330000 #330000 #330000 #330000; border-collapse: separate; background-color: #330000; #800517 f535aa #330000 school color #9A0000 school color2 #991B1E school color3 #CCCC99 school color4 #9A0000 } /*th is table header */ th { text-align: left; height: 2.5em; background-color: #330000; color: #FC0; font-size:1.5em; } /*td is table data or the cells below the header*/ td { text-align: left; height:1.0em; font-size:1.0em; vertical-align:bottom; padding:10px; border-width: 5px 5px 5px 5px; padding: 8px 8px 8px 8px; border-style: outset outset outset outset; border-color: #9A0000 #9A0000 #9A0000 #9A0000; background-color: #CCCC99; -moz-border-radius: 0px 0px 0px 0px; } </style> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Pick Which Rows you want to delete, Then press delete.</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> <td align="center" bgcolor="#FFFFFF">delete</td></tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this // edited if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> hi everyone. i'm wondering what the best way is to create a session variable and pass it to an iframe. i need to do something along these lines, but it doesn't seem to pass the ID. Any hints on how i should accomplish this? Code: [Select] session_start(); $_SESSION['ID']=$_GET['ID']; // id from previous page $ID=session_id(); <iframe src="iframepage.php?ID=<?php echo $ID; ?>" style="width:680px; height:200px;" noresize="noresize" frameborder="0" border="0" scrolling="Yes" allowtransparency="true" /> </iframe> I'm making a simple login system with MySQL and PHP (very simple, I'm just starting with PHP). The MySQL portion is done, but I need to ensure only people who are logged in can see certain content. To check if people are logged in, my website checks that they have the $_SESSION['user'] variable set. If it is set, then it lets them continue through the website, if not, it tells them to login. Is that enough security, or can people simply inject a session cookie into their browser to spoof that they are logged in? My idea was to generate a session key cookie when they login (just a random string of letters and numbers) and store that in the database, then on every page, check to make sure their session key is the same thing that's in the database. Is this necessary? It seems expensive. |