PHP not working in xampp, accessing through localhose - php

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$hfhf = 1;
$dd = 2;
echo( $hfhf + $dd );
?>
</body>
</html>
The above is saved as index.php in htdocs folder. I access it at http://localhost/index.php
The page is blank. When i view source I see exactly the code above.
Is there some setting I need to change in php.ini or xampp?
Thanks for the help!

Related

Is there a way to change default link in php?

The default link I get is to link index.php.
How can I change the default link to all my project to index.php?folder=0?
You can check GET starting in index.php file.
If $_GET['folder'] is not set then redirect otherwise load same page.
if(!isset($_GET['folder'])){
header("Location:./index.php?folder=0");
}
Hope this work to you.
You could do something like this...
<?php
$sec = 0;
$page = '?folder=0';
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
</body>
</html>

'; expected' error in define() function in PHP even though I already have it

I am trying to make simple PHP codes and encounter this error. However, when I run test it, everything is fine.
When I try to make a constant variable using define(), I put it like this.
define('NAME', 'Yoshi');
And the error appears next to the semicolon is: '; expected'.
Here is my full code.
<?php
// index.php
define('NAME', 'Yoshi');
$age = 30;
?>
<!DOCTYPE html>
<html>
<head>
<title> my PHP </title>
</head>
<body>
<h1>Something here</h1>
<div><?php echo NAME; ?></div>
<div><?php echo $age; ?></div>
</body>
</html>
When the PHP Intellisense extension is installed on VSCode you have to find these errors.

passing php variables from dynamically created HTML Document

How to pass the php variables from dynamically created HTML page to next php file.
For Example. I have the following php code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>
Now when I see dynamically created HTML page, it just shows me the following code with .html extension; so how can I pass the php variable from this page to next file:
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
try to replace
<a href='comments.php?id=`$uid`'>
to
<a href='comments.php?id=$uid'>
try this... Your mistake is here 'comments.php?id=$uid' is string no php code use correct like this 'comments.php?id=".$uid."'.
<?php
session_start();
$uid = $_SESSION['uid'];
$doc = new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=".$uid."'> comments</a>
</body>
</html>
");
echo 'wrote:' . $doc->savedHTMLFile("/home/user/project1/test1.html") . 'bytes';
?>
Basically, you can use $_GET[''] method to do it. You just insert it into the URL, but I think, this will make the site vulnerable to SQL Injection.
remove back ticks(`) around $uid or use this code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
comments
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>

Session data working local but not on remote server

this is my code. plain and simple.
1) first.html
<body>
<?php
session_start();
...
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
...
?>
</body>
1) second.html
<body>
<div id="map_canvas">
<script language="javascript" type="text/javascript">
<?php session_start(); ?>
some_render_function(<?php echo json_encode($_SESSION["somearray"]); ?>);
</script>
</div>
</body>
perfectly working code on localhost.
There are 2 facts that can help you guys to come up with where's the problem here.
1)If you check the source of the page second.html offline and online you can respectively see some_render_function('all the stuff from the json') and some_render_function(NULL)
2)If i check my shared server folder i can see a directory called php_session with apparently all the correct files in it (of all the sessions opened when i tested my project, with CORRECT data in it)
Any hints?
Hello session_start ( http://php.net/manual/en/function.session-start.php ) should always be the first parameter on your page ...
Example
First Page
<?php session_start();?>
<html>
<head>
<title>First</title>
</head>
<body>
<?php
$somearray = $Object->method($somevar);
$_SESSION["somearray"] = $somearray;
?>
</body>
</html>
Second Page
<?php session_start(); ?>
<html>
<head>
<title>Second</title>
</head>
<body>
<?php
var_dump($_SESSION["somearray"]);
?>
</body>
</html>
Session_start should be before any output. So, move <?php to the start of file. Otherwise, behaviour depends on server configuration.

php calling html

can you please let me know what I am missing:
<html>
<head><title>page</title></head>
<body>
<h2><center>Welcome to the mainpage</center></h2><br />
Home page
</body>
</html>
php code is :
<?PHP
$currpage=$_GET['currpage'];
echo "Hello world $currpage";
?>
when I click the homepage I want the sample4.php script to be executed and that output which is a html page to be displayed.
But when I click the homage page : I get a file window download.
I have the php script in the same location I have the html?
Does:
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>Home page<p>
</body>
</html>
Do what you want? Otherwise, you'll need to show us the PHP code, in case it's making it a download from within the PHP.
For a hello world program, try this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
$currpage = isset($_REQUEST['currpage']) && is_int($_REQUEST['currpage'])?(int)$_REQUEST['currpage']:1;
echo "<p>Current page is $currpage</p>\n";
?>
</body>
</html>
You may also need to make sure that PHP is installed and running on your machine/server...
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>

Categories