Read parameter from php in jsp - php

I have the following code snippet in php
if($userName==$dbUserName&&md5($passWord)==$dbPassWord){
echo "<input name='username' type='hidden' value='$userName'>";
header('Location: http://localhost:8080/ClientModule/student.jsp');
die();
}
the php redirects to the following jsp
<%#page contentType="text/html" pageEncoding="UTF-8" errorPage="error.jsp"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Home</title>
</head>
<body>
<h1>
Logged in as: ${param.username}
</h1>
<nav>
<p>Home</p>
<p>Profile</p>
<p>Teachers</p>
<p>Notifications</p>
</nav>
</body>
</html>
I must have done something wrong in the php file, can someone help me spot it?
Any help is much appreciated

When you do this:
header('Location: http://localhost:8080/ClientModule/student.jsp');
The browser simply does a GET request to the specified URL. The form field you echo out on the line above is not included in this request. In stead, what you want is something like this:
header('Location: http://localhost:8080/ClientModule/student.jsp?username='.$userName);

Related

Add html inside any tag without javascript

Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try

XAMPP: Changing content of PHP-file doesn't change what's shown in browser

First I had this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<h1>
<?php
echo "Hi again...";
?>
</h1>
</body>
</html>
I accessed the file through localhost/learningphp/myfirstfile.php and it rendered properly, showing me a h1 element with the text "Hi again...".
Then I changed to this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<p>
<?php
echo "Hi again...";
$myName = "Sahand";
echo $myName;
?>
</p>
</body>
</html>
Notice the change of <h1> tags to <p> tags, and the addition of myName. Still, when I go to localhost/learningphp/myfirstfile.php myName (Sahand) is not added to the page, and "Hi again..." is still shown in "h1 styling", like when I viewed the first version of the php file. Why is this and what can I do about it?
You will have two issues in this case may be
You are saving the file somewhere else or you didn't save the
updated content.
Your Browser history try a hard refresh by using Ctrl+F5 (for windows) Keys
together

Add multiple referrer links

I protect page from being access and can only access it by a referrer page, here is my code on landing page
<?php
// request file coming from test referrer
if(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php"))
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Test Landing Page 1</h1>
</body>
</html>
<?php
}
// redirect to redirect.php
else {
header("Location: http://aqsv.com/sites2/testlander/redirect.php");
}
?>
and this is the referrer page
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test Refererrer 1</title>
</head>
<body>
<h1>Test Refererrer 2</h1>
Link me to landing page1
</body>
</html>
this work perfectly on single referrer page only, what i want to do is to have multiple referrer page to access the page , Im new to php and really dont have any idea to do this.. I tried adding http referrer using if else like this
<?php
// request file coming from test referrer
if(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php"));
elseif(stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp2.php"))
{
?>
but the second link is not working. Any help would be highly appreciated. Thanks
You can take an array containing all your valid referrer domain. E.g.
<?php
$valid_domains = array(
'domain1',
'domain2',
'domain3'
);
// The checking for valid domain
if ( in_array($_SERVER['HTTP_REFERER'], $valid_domains) )
{
?>
Your HTML goes here....
<?php
}
?>
Please see http://php.net/manual/en/function.in-array.php
Hope the idea will help you.
The syntax if if/elseif... is:
if (something) {
body
} elseif (somethingelse) {
body
}
But you have no body for your if, only for the elseif, so nothing happens in that case.
Since you want the same body for all your tests, you should just use a single if with multiple conditions connected by OR:
if (stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp1.php") ||
stristr($_SERVER['HTTP_REFERER'],"http://aqsv.com/sites2/testreffer/tp2.php")) {
...
}
Another way you can do this is by putting all the allowed referers in an array, and then doing:
if (in_array(strtolower($_SERVER['HTTP_REFERER']), $allowed_referers)) {
...
}
I use strtolower() to make it case-insensitive, like your original tests.

PHP Warning: Cannot modify header information - headers already sent by [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I know this has been asked a million times already but I can't seem to find an answer that helps with mine.
I just started a new php website and as you saw in the title it keeps saying:
Warning: Cannot modify header information - headers already sent by (output started at /customers/e/7/8/andersws.dk/httpd.www/template/index.php:2) in /customers/e/7/8/andersws.dk/httpd.www/template/index.php on line 4
All that is in the file so far is:
<!DOCTYPE html>
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
So I really can't see what I'm doing wrong.
It's because you specified a doctype before the header. You cannot output a header after the payload has been sent. So change it to:
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
The HTTP headers can only being sent before the body. All output of the page belongs to the body. In your example you are attempting to output the header after the output of the <doctype> node.
Use this:
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
Use this;
<?php
ob_start();
if(file_exists('first.run')){
header('location: index.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
<?php ob_end_flush(); ?>
See here for more detail about `ob_start()

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