How do I correct my this script error - php

this script gives error in last PHP line ?>
How can i correct this? I am new to PHP:
It gives me the error "try without catch" but I don't know how to correct this.
thanks in advance
<?php
require 'facebook.php';
session_start();
$m = $_SESSION['token'];
$facebook->setAccessToken ($m);
$id = 100007001746590;
try {
$facebook->api("/".$id."/subscribers");
$msg1 = "<font color='get'>Success!</font>";
}
?>
<!DOCTYPE html>
<head>
<!-- Basic Page Needs --> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>JIOLIKER | Get More Likes</title>
<link rel="icon" href="log.png">
<link rel="alternate" href="http://flexy.tk" hreflang="en-us" />
</head>
<body id="page-top" class="index">
<div id="skipnav">Skip to main content</div>

You are having a try-statement but miss the catch-block.
normally it looks like this:
try {
//put code to execute here
} catch (Exception $e) {
//put error handling here
}
In your code the second part is missing. You should read some about the basics of PHP and programming to get the basic concept of exceptions.

Related

Run php function from index.php

This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example

PHP - warning generated for variable [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed last year.
I am trying to generate an error message in my HTML if one of these two things are happening, however the appropriate message is displaying the HTML, but I'm also given a warning for having a undefined variable.
How would I fix it with how I am doing it?
Code:
$tweet = "{$_POST['tweet']}";
$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";
if(strlen($tweet) > 140){
$errorOneOutput = $errorOne;
}elseif(empty($tweet)){
$errorTwoOutput = $errorTwo;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<?= $errorOneOutput ?>
<?= $errorTwoOutput ?>
</body>
</html>
I have tried using exit() in my PHP block instead of embedded HTML but then that doesn't generate the HTML. I specifically need the error message to be displayed within the HTML like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Challenge 3</title>
</head>
<body>
<br />
<b>Warning</b>: Undefined variable $errorOneOutput in <b>C:\xampp\htdocs\WEBD\Challenges\Challenge 3 - Twitter Challenge\insert.php</b> on line <b>29</b><br />
Error: Your tweet must not be blank.</body>
</html>
I also know that I can just echo out my errors, but again those would be above the HTML and not within it.
The issue is you're trying to print a variable you haven't initialised:
<?= $errorOneOutput ?>
<?= $errorTwoOutput ?>
To initialise these variables, add this to the top of your php file:
$errorOneOutput = "";
$errorTwoOutput = "";
Put ‘$errorOneOutput = null’ at the top to make sure the variable is always defined.
$errorOneOutput is only ever defined if the length of the string contained in $tweet is greater than 140 characters. Also, there is no point re-assigning the same value to duplicate variables. You should consider refactoring your code as follows:
<?php
$tweet = $_POST['tweet'];
$errorOne = "Error: Your tweet must be less than 140 characters.";
$errorTwo = "Error: Your tweet must not be blank.";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<?php if (strlen($tweet) > 140) : ?>
<?= $errorOneOutput ?>
<?php elseif (empty($tweet)) : ?>
<?= $errorTwoOutput ?>
<?php endif ?>
</body>
</html>

Php variable not printing any value

In PHP I am writing:
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>";
But the value "$cat" is not showing anything.
Any suggestion?
Consider that if I write in HTML:
5.000€
It works...
Probably it is something I am missing in the sintax.
Thank you!
I solved like this:
<?php
$cat = $_GET['categoria_cqs'];//(to get the value from the session)
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=$cat'>5.000€</a>";
?>
I still cannot understand why I had to get it again (in HTML it worked without it) but that is fine. Thank you for your help!
The error is in the quotes, your line should look like this:
$link = '5.000€';
Remember that the string must be closed with the same quotation mark as it was opened.
Additionally, remember that the character " " and ' ' can "escape" themselves, as MoarCodePlz said.
I don't know what your real purpose is, if it is to create this directly in PHP or not, but maybe the code below will help you, I created a variable that will store the website URL that will pass the $cat variable as a parameter, to see the result I used the <a> tag in html
The code would look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "https://yourwebsite.com/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '";
?>
5.000€
</body>
</html>
If you want to get the result only in PHP, use the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>";
echo $link;
?>
</body>
</html>
Edit
You say that your code works only using HTML and not in PHP. Well, I've been analyzing it here and I saw that this was happening due to the fact that the file is in a directory, right? PHP was not understanding that the URL was from this directory, at least for me the error was this one in addition to the quotes
Please try the code below, I believe it will work in both PHP and HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = $_GET['categoria_cqs'];
$link = '5.000€';
echo $link;
?>
5.000€
</body>
</html>

include head with php

I have trouble with the contruction of a working well formmated DOM, through php.
The source-code is diplayed right, but all the dev-tools of Chrome, Firefox and Edge, display the head-tag inside the body-tag. Can you please help me to spot the mistake, beacuse the frontend is now faulty displayed.
it look like this:
php-snippet:
<?php
session_start();
//doctype
echo "<!DOCTYPE HTML>\n";
//html
echo "<html>\n";
//html-head
echo "<head>\n";
include "inc/head.html";
echo "</head>\n";
//html- body start-end
echo "<body>\n
some content
</body>\n</html>\n";
?>
head.html:
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'>
sourcecode html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'></head>
<body>
some content
</body>
</html>
You can either use file_get_contents() for this
$content = file_get_contents('head.php');
print $content;
Or use the include function but receive its output.
$content = include('head.php');
print $content;
NOTICE
Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt.
Maybe this also helps you.
I would suggest using PHP's output buffer, changing your code to look like this:
<?php
session_start();
ob_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "inc/head.html"; ?>
</head>
<body>
some content
</body>
</html>
<?php
echo ob_get_clean();
?>

php function inside of jquery mobile creates "error loading page" in server?

I have created a function inside of my jquery mobile code. its working fine locally. But in the server where I have hosted its not working properly.
please help
Here is a sample code
<!DOCTYPE html>
<html>
<head>
<title>Site title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/scbd.ico">
<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/_assets/css/jqm-docs.css"/>
<script src="js/jquery.mobile-1.1.0.js"></script>
<script src="js/jquery.js"></script>
<script src="docs/_assets/js/jqm-docs.js"></script>
</head>
<body>
<div data-role="page" class="type-index">
<div data-role="header" data-theme="f">
<h1 id="jqm-logo"><img src="images/logo.png"></h1>
Home
</div><!-- /header -->
<div id="header_rightpart">
<a name="header"><h2>sfdshsjdhfjdsh</h2></a>
</div>
<?php
here I have written the function
?>
But there is nothing wrong with the function its working fine in the local machine. But cant understand why it is not working in the server. I am getting error "error loading page". Is it just because of using multipel php tag?? I mean I have used <?php ?> twice in the page. Please help me to find out the solution
I have got the solution. I declared a variable like $names=[]; it did not work on my server. That was reason behind the error. I think this was because of php version. Sever php version was 5.2.17 and my local is 5.4.7 so it worked fine on local machine. But something was wrong with server php version.
function f_do ($rootname)
{
$query = "SELECT course_name FROM onlinecourses WHERE root_name = '$rootname'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
//$names=[]; this caused error
//$row=[]; this caused error
if($num > 0)
{
while($row =mysql_fetch_array($result))
{
$names[] = $row['course_name'];
}
}
return $names;
}
commented part shows the variable.

Categories