fgets() returns false everytime php - php

my project's task is simple: read from a file line by line, check if the user and password are stored into "database.txt".
the problem is that "fgets($fd)" returns everytime "FALSE" instead of the string, so when i use strpos or substr the return value is always false.
this is my code, thanks for the help:
<?php
function signin(){
if($_SERVER['REQUEST_METHOD']=="POST"){
if(empty($_REQUEST['loginuser']) || empty($_REQUEST['loginpsw']))
return false;
else{
$fd = fopen("database.txt","r");
while(!feof($fd)){
$line = fgets($fd);
if(strpos($line,$_REQUEST['loginuser']) !== false){
if(substr($line,strrpos($line,",")+1) === sha1($_REQUEST['loginpsw'])){
fclose($fd);
return true;
}
}
}
fclose($fd);
return false;
}
}
else return false;
}
if(!signin()){
header("Location: index.html");
}
else {
header("Location: home.php");
}
?>
<html>
<head>
<title>Test Page</title>
<meta name="keywords" content="testpage test page keywords aristocats pompelmi cavolfiore" />
<meta name="author" content ="Alessandro Savona" />
<link rel="stylesheet" type="text/css" href="css/Style.css" />
<link href="https://fonts.googleapis.com/css?family=Bangers" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet">
</head>
<body>
<div class="topbar">
<div class="logo">
<label>We Like Pepperoni</label>
</label>
</div>
<div class="logo2">
<img src="https://www.trieste.pizza/resources/images/trieste-pizza-logo.png">
</div>
</div>
</body>
</html>
again, thank you for the help!
EDIT:
the value of $fd is 5
it's located in the path /var/www/html
that is the file:
###########################################################
# name surname,username,password SHA1(Rowling) #
###########################################################
Harry Potter,potter,f5043f5cb1f52071e96a44303919b88179eb7bad
Ron Weasley,weasley,f5043f5cb1f52071e96a44303919b88179eb7bad
Hermione Granger,granger,f5043f5cb1f52071e96a44303919b88179eb7bad
Lord Voldemort,voldemort,f5043f5cb1f52071e96a44303919b88179eb7bad
Albus Dumbledore,dumbledore,f5043f5cb1f52071e96a44303919b88179eb7bad
Severus Snape,snape,f5043f5cb1f52071e96a44303919b88179eb7bad
Rubeus Hagrid,hagrid,f5043f5cb1f52071e96a44303919b88179eb7bad
Draco Malfoy,malfoy,f5043f5cb1f52071e96a44303919b88179eb7bad

Related

Part of PHP only runs if the form is submitted twice

In a very basic login page I set up an error status to show if the form was submitted with an empty pin or an invalid pin was entered but it only functions if I submit the form twice with the same error.
Where am I going wrong here?
Or else what would be a better way to achieve this?
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
?>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
</head>
<body>
<h1>Page Title</h1>
<div class="smallcontainer">
<form method="post" action="">
<div class="row">
<div class="col-20">
<label for="id">Driver Pin</label>
</div>
<div class="col-80">
<input type="text" name="id" >
</div>
</div>
<div class="row">
<div class="col-100 error">
<?= #$_SESSION['status'] ?>
</div>
<div class="col-100">
<input type="submit" value="Submit" name="submit">
</div>
</div>
</form>
</div>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$id = trim($_POST['id']);
if (empty($_POST['id'])){
$_SESSION['status'] = 'Enter a Driver Pin';
}
// if (!strlen($id)) {
// $_SESSION['status'] = 'Enter a Driver Pin';
// //die('Please enter Driver Pin');
// }
$success = false;
$handle = fopen("users.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
if ($data[0] == $id) {
$success = true;
$_SESSION['displayname'] = $data[1];
break;
}
}
fclose($handle);
if ($success) {
$_SESSION['allow'] = '';
header('Location: ./');
} else {
$_SESSION['status'] = 'Invalid Driver Pin - Try again';
}
}
?>
First you need to put the php before the form (as already said) and here is your code with a few fixes:
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (isset($_POST['submit'])) {
$id = trim($_POST['id']);
$success = false;
$handle = fopen("users.csv", "r");
while (($data = fgetcsv($handle)) !== FALSE) {
if ($data[0] == $id) {
$success = true;
$_SESSION['displayname'] = $data[1];
break;
}
}
fclose($handle);
if ($success) {
$_SESSION['allow'] = '';
$_SESSION['status'] = 'Success!';
/*header('Location: ./');*/
} else {
if (empty($_POST['id'])){
$_SESSION['status'] = 'Enter a Driver Pin';
} else {
$_SESSION['status'] = 'Invalid Driver Pin - Try again';
}
}
} else {
$_SESSION['status'] = 'Enter a Driver Pin';
}
?>
<html>
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
</head>
<body>
<h1>Page Title</h1>
<div class="smallcontainer">
<form method="post" action="">
<div class="row">
<div class="col-20">
<label for="id">Driver Pin</label>
</div>
<div class="col-80">
<input type="text" name="id" >
</div>
</div>
<div class="row">
<div class="col-100 error">
<?= #$_SESSION['status'] ?>
</div>
<div class="col-100">
<input type="submit" value="Submit" name="submit">
</div>
</div>
</form>
</div>
</body>
</html>
This will work - notice I added an else if there is no post on fresh load you need to reset the $_SESSION['status'];
You php that checks and set status is located after HTML, which means the HTML returned by the page submission will not have the status updated until next submission.
So, move your PHP code above HTML.
Also, you can't use header() after anything was printed out already.

How do I get the template file for a single.php pods page?

I am currently using a template file called single-product.php and want to check for page template in header.php. I've tried using get_page_template_slug($post->ID) but it returns empty. I've also tried is_page_template('single-product.php'); but that returns nothing, as well.
Here's my header. You'll notice that I'm trying to get the template file through s_page_template('single-product.php') ny setting it as the value of $isProductPodTemplate at the top and use it in the if statement at the bottom:
<?php
$page = get_the_title($post->ID);
$frontpageId = get_option('page_on_front');
$isStandardTemplate = is_page_template('standard.php');
$isContactTemplate = is_page_template('contact.php');
$isProductPodTemplate = is_page_template('single-product.php');
?>
<!DOCTYPE html>
<html id="mainHTML" lang="en">
<head>
<title>Architectural Iron Works</title>
<meta name="description" content="Architectural Iron Works">
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel=icon type="img/ico" href=/favicon.ico> -->
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/main.bundle.css" />
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<?php wp_head(); ?>
<script type="text/javascript">
document.getElementById("mainHTML").style.display = "none";
</script>
</head>
<body>
<!-- Header -->
<?php
if ($post->ID == $frontpageId)
{
get_template_part('/includes/_home-header');
echo "<div class='main-content'>";
}
else if (is_404() ||
$isContactTemplate ||
$isStandardTemplate)
{
get_template_part('/includes/_no-banner-header');
echo "<div class='main-content no-banner'>";
}
else if ($isProductPodTemplate)
{
// do something
}
else
{
get_template_part('/includes/_default-header');
echo "<div class='main-content'>";
}
?>

echo not passing var passed while

I am trying to get echo $userRow['student_firstname']; to pass into the html so I can use it as a automatic way to change displaying the users first name & logout with register & login. The first echo $userRow['student_firstname']; does work, however the second does not. The idea would be able to get the second to work, so I can remove the first. If there isn't a session then it displays the login & register.
<?php
session_start();
include_once 'dbconnect_new.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
<link href="favicon.png" type="image/x-icon" rel="shortcut icon">
<link href="assets/css/master.css" rel="stylesheet">
<script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
</head>
<?php if(isset($_SESSION['user'])) {
// error_reporting(E_ALL ^ E_DEPRECATED);
$res=mysql_query("SELECT * FROM studentdata WHERE student_id=".$_SESSION['user']);
if ($res === FALSE){
die(mysql_error());
}
while($userRow=mysql_fetch_array($res))
{
echo $userRow['student_firstname'];
}
?>
<li class="dropdown">
<a href="">
<?php echo $userRow['student_firstname'];?><span class="nav-subtitle">Account</span></a</li>
<li>Logout<span class="nav-subtitle">Goodbye</span></li>
<?php } else { ?>
<li>Register<span class="nav-subtitle">for Students</span></li>
<li>Login<span class="nav-subtitle">for Students</span></li>
<?php } ?>
</html>
Check this link out http://php.net/manual/en/function.mysql-fetch-array.php,
Tip: USE mysqli/pdo as mysql is deprecated.
<?php
session_start();
include_once 'dbconnect_new.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
<link href="favicon.png" type="image/x-icon" rel="shortcut icon">
<link href="assets/css/master.css" rel="stylesheet">
<script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
</head>
<?php
if (isset($_SESSION['user'])) {
// error_reporting(E_ALL ^ E_DEPRECATED);
$res = mysql_query("SELECT * FROM studentdata WHERE student_id=" . $_SESSION['user']);
if ($res === FALSE) {//IF QUERY RETURNS NULL
die(mysql_error());
} else {
$userRow = mysql_fetch_array($res, MYSQL_ASSOC); //DATA IN ARRAY TYPE
//IF BELOW DOES NOT WORK USE print_r(); to check the structure of array !
?>
<li class="dropdown">
<?php echo $userRow['student_firstname']; //ACCESS DATA THROUGH ITS INDEX ?><span class="nav-subtitle">Account</span>
</li>
<li>
Logout<span class="nav-subtitle">Goodbye</span>
</li>
<?php
}
} else {
?>
<li>Register<span class = "nav-subtitle">for Students</span></li>
<li>Login<span class = "nav-subtitle">for Students</span></li>
<?php } ?>
</html>
I Hope this works !

PHP code in body tags

I tried putting some php in the body tag to change the background color of the page based on what value was passed from the first page but it's not working.
Can I do this? (Add php code inside body tag)
If not, what would be the easiest way to change the background color of the page based on the value passed from the first page
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset=utf-8>
<title></title>
</head>
<body <?php
$teamvar = $_POST['team_id'];
if($teamvar == 1){
$teamvar = "Celtics";
echo "style='background-color: green;'>"
} else {
echo "style='background-color: yellow;'>"; }
?>
<div id=content>
<p>
Test
</p>
</div>
</body>
</html>
You missed a semi-colon ; at the end of echo "style='background-color: green;'>"
Change it to:
echo "style='background-color: green;'>";
and it will work.
You should also put quotes for <div id=content>
<div id="content">
which is just good form.
This code looks like it should work to me. Are you sure you're posting the correct value? Try printing out the posted value on the page to see. Or just var_dump($_POST);.
However as it is your code isn't very readable. It's best not to intermingle HTML and logic. I'd do something like this:
<?php
$teamid = $_POST["team_id"];
if ($teamid == "1"){
$color = "green";
} else {
$color = "yellow";
}
?>
...
<body style="background-color: <?= $color %>;">
Try following code
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset=utf-8>
<title></title>
</head>
<?php
$body_style = "";
$teamvar = $_POST['team_id'];
if($teamvar == 1){
$teamvar = "Celtics";
$body_style ="style='background-color: green;'>";
} else {
$body_style = "style='background-color: yellow;'>";
}
?>
<body <?php echo $body_style; ?> >
<div id="content">
<p>
Test
</p>
</div>
</body>
</html>
Your code isn't wrong, but you can use it in a better way, for example.
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset=utf-8>
<title></title>
</head>
<?php
if (isset($_POST['team_id']) {
if($_POST['team_id'] == 1)
$variable = "style='background-color: green;'>";
else
$variable = "style='background-color: yellow;'>";
} else
$variable = "style='background-color: yellow;'>";
?>
<body <?php echo $variable;?>>
<div id=content>
<p>
Test
</p>
</div>
</body>
</html>
As you can see, this code is more aesthetic than yours.
Here is you working code
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset=utf-8>
<title></title>
</head>
<body <?php
$teamvar = $_POST['team_id'];
if($teamvar == 1){{
$teamvar = "Celtics";
echo "style='background-color: green;'>";
} else {
echo "style='background-color: yellow;'>"; }
?>
<div id=content>
<p>
Test
</p>
</div>
</body>
</html>
you can test it :)
Your code looks bad! But this should work:
(BTW: You forgot a ; in your code)
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset="utf-8">
<title>Test</title>
</head>
<?php
//Test
$_POST['team_id'] = 1;
$teamvar = $_POST['team_id'];
if($teamvar == 1) {
$teamvar = "Celtics";
echo "<body style='background-color: green;'>";
} else {
echo "<body style='background-color: yellow;'>";
}
?>
<div id="content">
<p>Test</p>
</div>
</body>
</html>
Also i would recommend you to turn on error reporting with this:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>

How to redirect my pages to index.php?

I would like to know why when I go on my site http://nextgenfocus.com/, index.php does not in the URL and the content is not fully displayed. I tried with a .htaccess file, but nothing helped.
My index.php file:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<?php
if(strpos($_SERVER["REQUEST_URI"], "index.php") !== false) { ?>
<title>Test - Home</title>
<?php } ?>
<?php
if(strpos($_SERVER["REQUEST_URI"], "downloads") !== false) { ?>
<title>Test - Downloads</title>
<?php } ?>
<?php
if(strpos($_SERVER["REQUEST_URI"], "help") !== false) { ?>
<title>Test - Help</title>
<?php } ?>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
How I can do, please?
try to configure your httpd config and add DirectoryIndex to default execute index.php if found in that path.
DirectoryIndex index.php index.phtml index.html index.htm
Anyway, if you really want to redirect via Apache you should paste this code in your .htaccess
RewriteEngine on
Redirect / /index.php
BE SURE that your apache has mod_rewrite enabled
Add this in your container.php file
<?php if((strpos($_SERVER["REQUEST_URI"], "index.php") !== false) && (strpos($_SERVER["REQUEST_URI"], "/") !== false)){ ?>
<div id="container">
<!-- Your code here -->
</div>
<?php } ?>
You're trying to match on request uri, that is what you read on your browser address box. You shold match on $_SERVER['SCRIPT_FILENAME'] index
you could use this code for matching your page:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<?php
if(strpos($_SERVER["SCRIPT_FILENAME"], "index.php") !== false) { ?>
<title>Test - Home</title>
<?php } ?>
<?php
if(strpos($_SERVER["SCRIPT_FILENAME"], "downloads") !== false) { ?>
<title>Test - Downloads</title>
<?php } ?>
<?php
if(strpos($_SERVER["SCRIPT_FILENAME"], "help") !== false) { ?>
<title>Test - Help</title>
<?php } ?>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>

Categories