php header function isn't working - php

I've tried to use ob_start(); and exit(); ,both are not working, please advise, thank you very much
<?php
ob_start();
// connect to the database
include('connect-db.php');
// check if the form has been submitted.
// If it has, start to process the form and save it to the database
// once saved, redirect back to the view page
if (isset($_POST["submit"]))
{
foreach ($_POST['patientid'] as $index => $patientid)
{
$id = mysql_real_escape_string($_POST['id'][$index]);
$data1 = mysql_real_escape_string($patientid);
$data2 = mysql_real_escape_string($_POST['vaccineid'][$index]);
$data3 = mysql_real_escape_string($_POST['vaccinename1'][$index]);
$data4 = mysql_real_escape_string($_POST['vaccinename2'][$index]);
$data5 = mysql_real_escape_string($_POST['vaccinename3'][$index]);
$data6 = mysql_real_escape_string($_POST['totalnoofinjection'][$index]);
$data7 = mysql_real_escape_string($_POST['nthinjection'][$index]);
$data8 = mysql_real_escape_string($_POST['date'][$index]);
$data9 = mysql_real_escape_string($_POST['nextdate'][$index]);
$data10 = mysql_real_escape_string($_POST['skip'][$index]);
$data11 = mysql_real_escape_string($_POST['language'][$index]);
mysql_query("UPDATE patientvaccinedetail SET patientid = '$data1',
vaccineid = '$data2', vaccinename1 = '$data3',
vaccinename2 = '$data4', vaccinename3 = '$data5',
totalnoofinjection = '$data6', nthinjection = '$data7',
date = '$data8', nextdate = '$data9', skip = '$data10',
language = '$data11'
WHERE id=$id") or die(mysql_error());
header("Location: start.php");
exit;
}
}
Just updated and still cant't redirect to another pages

You are missing semi colon after exit
Corrected code:
exit;

use ob_end_clean(); before the header call and use exit; instead of exit
try like below
ob_end_clean();
header("Location: start.php");
exit;

Related

can't upload multiple image with use validation mysql

I need your help guys. My code updates only the first image. When I try to update two more images my code doesn't work.
This my code
<?php
include ("config.php");
$id= $_POST['id'];
$judul = $_POST['judul'];
$tanggal = $_POST['tanggal'];
$konten1 = $_POST['konten1'];
$konten2 = $_POST['konten2'];
$konten3 = $_POST['konten3'];
$posisi=$_POST['posisi'];
$posisi2=$_POST['posisi2'];
$tema=$_POST['tema'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
$lokasi_foto1 = $_FILES['foto1']['tmp_name'];
$nama_foto1 = $_FILES['foto1']['name'];
$lokasi_foto2 = $_FILES['foto2']['tmp_name'];
$nama_foto2 = $_FILES['foto2']['name'];
$lokasi_logo = $_FILES['logo']['tmp_name'];
$nama_logo = $_FILES['logo']['name'];
if(isset($_FILES['logo'] ) && ($_FILES['banner']) && $_FILES['banner']['name'] !="" && $_FILES['logo']['name'] !="") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['logo'];
$e = 'upload/'.$r['banner'];
unlink ($d);
unlink ($e);
move_uploaded_file($lokasi_logo,"upload/".$nama_logo);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file' , judul='$judul', tanggal='$tanggal', posisi='$posisi',konten1='$konten1', konten2='$konten2', konten3='$konten3', tema='$tema' ,posisi2 = '$posisi2', footer='$footer' , logo='$nama_logo' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
}
if ($edit = mysql_query("UPDATE newsletter SET judul='$judul', tanggal='$tanggal', posisi='$posisi',konten1='$konten1', konten2='$konten2', konten3='$konten3' , tema='$tema' ,posisi2 = '$posisi2', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
So if I try to update the banner and the logo, my code just updates the banner. The Logo is not updated. If anyone can help , I will be happy :)
EDIT :
The problem on this my code is on after if (isset) I am confuse how to use multiple isset with use that
You're using exit(), meaning that when the banner is uploaded, the script will terminate. Unless there's more code to this, there's no need to use exit()
Source: PHP exit()

adding counter to php page to count the unique visitors

I want to add a counter in my webpage which counts the number of visitors.
But my problem is that when i refresh my page ,counter increases by 1..i want that counter increases only when a new visitor with another ip reaches to my webpage.
here are my codes..
Sorry for my weak english
index.php
<?php
session_start();
$ip = $_SERVER['REMOTE_ADDR'];
$_SESSION['current_user'] = $ip;
if(isset($_SESSION['current_user']))
{
$count = file_get_contents("counter.txt");
$count = trim($count);
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
}
else
{
$count = file_get_contents("counter.txt");
$count = trim($count);
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
}
As database based solution is not preferred, You can try the following file based solution for counting unique visitor. You already have used counter.txt file in your code.
I tried to use the same file that you have used. In my case I am storing IP address in that file. I have used base64 encoding function just to hide the IP address. It is always good to keep that file in a safe place. If that file is lost then the unique visitor IPs will be lost. See the function below:
Function definition
function getUniqueVisitorCount($ip)
{
session_start();
if(!isset($_SESSION['current_user']))
{
$file = 'counter.txt';
if(!$data = #file_get_contents($file))
{
file_put_contents($file, base64_encode($ip));
$_SESSION['visitor_count'] = 1;
}
else{
$decodedData = base64_decode($data);
$ipList = explode(';', $decodedData);
if(!in_array($ip, $ipList)){
array_push($ipList, $ip);
file_put_contents($file, base64_encode(implode(';', $ipList)));
}
$_SESSION['visitor_count'] = count($ipList);
}
$_SESSION['current_user'] = $ip;
}
}
Function call
$ip = '192.168.1.210'; // $_SERVER['REMOTE_ADDR'];
getUniqueVisitorCount($ip);
echo 'Unique visitor count: ' . $_SESSION['visitor_count'];
Output
Unique visitor count: 2
Change:
if(isset($_SESSION['current_user']))
to:
if($_SERVER['REMOTE_ADDR'] == $_SESSION['current_user'])
And, surely you dont need to get $count from a file, and then write the same value back to the file...? If the $_SERVER['REMOTE_ADDR'] matches the SESSION['current_user'] then do nothing..
try to store the user IP in database and check for unique user,
<?php
session_start();
if (!$_SESSION['status']) {
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("ip_log", $connection);
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `database`.`table` (IP) VALUES ('$ip')");
mysql_close($connection);
$_SESSION['status'] = true;
}
?>
Best And Easy Code
Try to store the user IP in database and check for unique user
$`servername` = "";
$username = "";
$password = "";
$`dbname` = "";
$`conn` = new `mysqli`($`servername`, $username, $password, $`dbname`);
if ($`conn`->connect_error) {
die("Connection failed: " . $`conn`->connect_error);
}
$address = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$name = `gethostname`();
$re = "select * from visitor where name='$name'";
$call = `mysqli_fetch_array`($re);
$as = `mysqli_num_rows`($call);
if($as == 0){
$`sql` = "UPDATE visitor SET visits = visits+1 WHERE name = '$name'";
}else{
$`sql` = "INSERT INTO visitor(visits,name,address) VALUE(1,'$name','$address')";
}
$`conn`->query($`sql`);
$`sql` = "SELECT visits FROM visitor WHERE id = 1";
$result = $`conn`->query($`sql`);
if ($result->`num_rows` > 0) {
while($row = $result->fetch_assoc()) {
$visits = $row["visits"];
}
} else {
$visits = "";
//echo $visits;
}
`$conn`->close();

Sessions not being created by form?

I am trying to create a session for each field of a form so that I can call it back in on the additional information form on the next page.
<?php
session_start(); // starting the session
if (isset($_POST['Submit'])) {
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
}
?>
Using some code and after a few modifications I have the code above trying to store data that has been submitted in the form..
How can I improve this code to make it work?
If it is not working, try debugging.
If it is saving the POST, then there is not much left to do.
Set session.cookie.secure to true. You may want to set session.cookie_lifetime.
As far as security, consider if you have something worth protecting. Does it matter if someone gets a visitor's session cookie? If not, forget it.
session_set_cookie_params(0, '/', '', true, false);
session_start();
error_reporting(E_ALL); // debug
if (isset($_POST['Submit'])) {
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
} else {
echo '<h3>Session Not Saved</h3>';
}
echo htmlentities(var_export($_REQUEST, true)); // debug
echo htmlentities(var_export($_SESSION, true)); // debug
DEBUG
It should work - if not, test and debug.
Show all Warnings.
Check $_SESSION after setting.
Check Request.
$_REQUEST includes $_COOKIE which should contain a SESSION cookie.
We need to regenerate id. Try this. (Sorry for bad english)
<?php
session_start(); // starting the session
if (isset($_POST['Submit'])) {
session_regenerate_id();
$_SESSION['breakdowndate'] = $_POST['breakdowndate'];
$_SESSION['policyinception'] = $_POST['policyinception'];
$_SESSION['customername'] = $_GET['customername'];
$_SESSION['customersurname'] = $_GET['customersurname'];
$_SESSION['covertype'] = $_POST['covertype'];
$_SESSION['vehiclemake'] = $_POST['vehiclemake'];
$_SESSION['vehiclemodel'] = $_POST['vehiclemodel'];
$_SESSION['vehiclereg'] = $_POST['vehiclereg'];
$_SESSION['vehicleage'] = $_POST['vehicleage'];
$_SESSION['excess'] = $_POST['excess'];
$_SESSION['mileage'] = $_POST['mileage'];
$_SESSION['paid'] = $_POST['paid'];
$_SESSION['HSRS'] = $_POST['HSRS'];
$_SESSION['fault'] = $_POST['fault'];
$_SESSION['garage'] = $_POST['garage'];
$_SESSION['telephone'] = $_POST['telephone'];
session_write_close();
}
?>
Store your variables in an array and loop as shown below. You can use session_start(); on your next page and access them:
<?php
session_start(); // starting the session
$sessionItems = array('breakdowndate','policyinception','customername'...'telephone');
if (isset($_POST['Submit'])) {
foreach($sessionItems as $item) {
$_SESSION[$item] = $_POST[$item];
}
}
?>

PHP & MYSQL: Echo not working

I used to store all my data in 000webhost, today I decided to move to hostinger. So.. after moving it I replaced the old mysql_connect info by the new one. Alright, after doing that I tested it, everything has ran fine, except some echo functions.
check file (connects to the server and do the login):
<?php
$servidorr = "mysql.XXXX.co.uk";
$bdd = "XXXXXXXX";
$usuarioo = "XXXXX";
$senhaa = "XXXXXXX";
if (!empty($_POST) AND (empty($_POST['usuario']) OR empty($_POST['senha']))) {
header("Location: geton"); exit;
}
mysql_connect($servidorr, $usuarioo, $senhaa) or trigger_error(mysql_error());
mysql_select_db($bdd) or trigger_error(mysql_error());
$usuario = mysql_real_escape_string($_POST['usuario']);
$senha = mysql_real_escape_string($_POST['senha']);
$lang = mysql_real_escape_string($_POST['lang']);
$sql = "SELECT `id`, `nome`, `nivel` FROM `usuarios` WHERE (`usuario` = '". $usuario ."') AND (`senha` = '". sha1($senha) ."') AND (`ativo` = 1) LIMIT 1";
$updatelang = "UPDATE usuarios SET lang='$lang' WHERE usuario='$usuario'";
$query = mysql_query($sql);
if (mysql_num_rows($query) != 1) {
echo "<script>alert('Oops! Looks like there is something wrong with your login! *perhaps a typo or you did not fill out the fields*'); location.href='geton'</script>"; exit;
} else {
$resultado = mysql_fetch_assoc($query);
mysql_query($updatelang);
if (!isset($_SESSION)) session_start();
$_SESSION['UsuarioID'] = $resultado['id'];
$_SESSION['UsuarioNome'] = $resultado['nome'];
$_SESSION['usuario'] = $resultado['usuario'];
$_SESSION['UsuarioNivel'] = $resultado['nivel'];
$_SESSION['lang'] = $resultado['lang'];
header("Location: http://mapmaking.zz.mu/pages/home"); exit;
}
?>
Home file (these echos are just for testing and this is not the original file, the original one has the same php stuff, except the echo functions, those are in random lines):
<?php
if (!isset($_SESSION)) session_start();
$tlang = $_SESSION['UsuarioLang'];
$aclevel = $_SESSION['UsuarioNivel'];
$nick = $_SESSION['UsuarioNome'];
$neededal = 1;
if (!isset($_SESSION['UsuarioID']) OR ($_SESSION['UsuarioNivel'] < $neededal)) {
session_destroy();
header("Location: http://inside.mapmaking.uk.to/geton"); exit;
}
session_start();
echo $tlang;
echo $aclevel;
echo $nick;
echo "$level$tlang$tlang";
?>
[this one basically start the session and check if the connected user acess level is 1]
Echo $tlang does not work! :( somehow it doesn’t, I have no idea why ;_;
Hope you guys help me, thank you!!
$_SESSION['lang'] != $_SESSION['UsuarioLang']
You assign a value to the first one, yet expect value from the second one.
$_SESSION['lang'] = $resultado['lang'];
$tlang = $_SESSION['UsuarioLang'];
Change this line:
$_SESSION['lang'] = $resultado['lang'];
to the following:
$_SESSION['UsuarioLang'] = $resultado['lang'];
You should also call session_start() without the isset check. Also, you should consider using && instead of AND and || instead of OR, as PHP has weird operator precedence rules (the assignment = has a higher precendence than either AND or OR).

why my application is running only when ob_start() method is used

I have written a simple application which has login page. After login search insertion buttons are there. I build this simple application and run successfully last month. But now when i am using this now my pages are not redirecting to next page. i double checked my syntax.
i googled for the answer and found ob_start() method. And now my application is running successfully.
when the first time i have used this application i didn't use ob_start. But why its not working now without the ob_start(); method.
Please explain.
Search function :
$email = $_POST['search'];
$sql = "select * from employee_details where ";
if($email){
$sql .= " email = '$email' ";
}
$rv = mysql_query($sql);
if(mysql_num_rows($rv) == 0){
$errmsg_arr[] = "Employee records are not found.";
$page = "HomePage.php";
$errflag = true;
errors($page,$errmsg_arr, $errflag );
}
while( $row = mysql_fetch_array($rv)){
$var['employee_id'] = $row['employee_id'];
$var['employeename'] = $row['employee_name'];
$var['email'] = $row['email'];
$var['phone'] = $row['phone'];
$var['address1'] = $row['address1'];
$var['state'] = $row['state'];
$var['country'] = $row['country'];
}
addvars($var);
header("location: EmployeeDetails.php");
addvars function:
function addvars($vars) {
session_start();
$_SESSION[] = array();
foreach($vars as $keys => $vals) {
$_SESSION[$keys] = $vals;
}
}
Errors Function:
function errors($page,$errmsg_arr, $errflag){
if($errflag) {
session_start();
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: $page");
exit();
}
}
Most likely because you try to set headers when they're already sent out. (you can try to adjust output buffering instead of using ob_start if it bothers you).

Categories