Is there a way to change default link in php? - 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>

Related

PHP not working in xampp, accessing through localhose

<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!

upon automatic reloading page error is undefined index in php

I am beginner to php.. please help,,
I am passing data from one page to another using GET
I am reloading the page automatically using following code.
<?php require_once ("includes/sessions.php"); ?>
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "100000";
?>
<?php include ("includes/connection.php"); ?>
<?php
conferm_login();
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<?php
$usr = $_GET['usr'];
$usr2='';
if($usr=='User1')
$usr2='User2';
it works fine but after the automatic refresh the error i am getting is Unidentified index.pointing to variables i.e $user=$_GET['usr'] even in previous pages from where i send data. I also wrapped $user=$_GET['usr'] with if(isset($_GET['usr'])) but there will no output.
<?php
require_once ("includes/sessions.php");
include ("includes/connection.php");
$page = basename($_SERVER['REQUEST_URI']);
$sec = "100000";
conferm_login();
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<?php
$usr = $_GET['usr'];
$usr2='';
if($usr=='User1')
$usr2='User2';
?>

Read parameter from php in jsp

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);

PHP - auto refreshing page

I am using following code for a refreshing page, it is not reloading on completion. The following code is not working sometime.
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
echo "Watch the page reload itself in 10 second!";
Use a <meta> redirect instead of a header redirect, like so:
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
<?php
echo "Watch the page reload itself in 10 second!";
?>
</body>
</html>
you can use
<meta http-equiv="refresh" content="10" >
just add it between the head tags
where 10 is the time your page will refresh itself
use this code ,it will automatically refresh in 5 seconds, you can change time in refresh
<?php
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url1");
?>
Try out this as well. Your page will refresh every 10sec
<html>
<head>
<meta http-equiv="refresh" content="10; url="<?php echo $_SERVER['PHP_SELF']; ?>">
</head>
<body>
</body>
</html>
Maybe use this code,
<meta http-equiv="refresh" content = "30" />
take it be easy
This works with Firefox Quantum 60+ and Chrome v72 (2019)
//set a header to instruct the browser to call the page every 30 sec
header("Refresh: 30;");
It does not seem to be NECESSARY to pass the page url as well as the refresh period in order to (re)call the same page. I haven't tried this with Safari/Opera or IE/Edge.
Simple step like this,
<!DOCTYPE html>
<html>
<head>
<title>Autorefresh Browser using jquery</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(function() {
startRefresh();
});
function startRefresh() {
setTimeout(startRefresh,100);
$.get('text.html', function(data) {
$('#viewHere').html(data);
});
}
</script>
</head>
<body>
<div id="viewHere"></div>
</body>
</html>
This video for complete tutorial
https://youtu.be/Q907KyXcFHc
<meta http-equiv="refresh" content="10" >
This can work. Try it..!! :-)

PHP: How can I reference variables from an included file before it's been included?

How can I reference variables from an included file before it's been included? Or can I somehow include the file (so I can lead its variables later) before its HTML is literally inserted into the body tag? Or can I contain all of home's body content in one big variable that I can echo as well in the index?
Here's what I'm trying to do:
index.php
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<?php
$title="home page";
$description="this is the home page";
$keywords="home, awesome, yes";
?>
this is the home page content that gets inserted into the body!
Just move the include statement to the top of the file.
This will expose all values, functions and variables to all subsequent lines.
<?php include 'home.php'; ?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
</body>
</html>
Short answer version: You can't. You'll get an 'Undefined variable' notice if you do that.
I find it is usually much more convenient to have a header.php (and a footer.php for that matter) which gets included in the index, home, contact or whatever other file. The advantage is that you don't have redundant code, and if you need to make a modification in the header or footer, you need to only modify one file.
So for example, 'about_us.php' would look like:
<?php
include('path/to/header.php');
#body goes here
include('path/to/footer.php');
?>
And your header would be something like:
<?php
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4));
?>
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="this is the home page" />
<meta name="keywords" content="home, awesome, yes" />
</head>
<body>
The $title variable will be the file name, minus the extension, with all underscores replaced by spaces and the first letter of the first word capitalized. So basically about_us.phpwould be converted into "About us". This is not necessarily a general solution, but I gave it as an example keeping in mind that you wanted to use a dynamic title in your original example. For dynamic description and keywords, based on the file name you could also assign different values with the help of a switch() statement.
UPDATE:
Another solution, although kind of the reverse of what you're asking, but at the same time much closer to what you're looking for would be to write the header.php like
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="<?php echo $desc; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
... the footer like ...
</body>
</html>
... and then include them in your other files:
<?php
$title = 'Your title';
$desc = 'Your description';
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog';
include('path/to/header.php');
?>
<!-- body goes here -->
<?php
include('path/to/footer.php');
?>
This way, you are assigning all the variables BEFORE you are including the files in which they are being referenced, you have distinct files for all the links and you don't need fancy switches. Also as a side note, wrapping the body's HTML in PHP is simply bad practice. Try to keep the HTML separated from the PHP as much as possible in general. It will help both you, and whoever is going to do work on the code in the future.
Hope this helps !
I would have a look at using a template system. Separating your code from the content will save you a lot of trouble in the future. it will also allow you to change the html template easily in the future. plus you can see your template without having to run the php code.
have a look at smarty templates
http://www.smarty.net/
you would then build a template file: "template.tpl"
<html>
<head>
<title>{$title}</title>
<meta name="description" content="{$description}" />
<meta name="keywords" content="{$keywords}"/>
</head>
<body>
{$home_content}
</body>
</html>
and some php code to run:
<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('title' , 'Your title');
$smarty->assign('description' , 'Your description');
$smarty->assign('keywords' , 'The, big, brown, fox, jumps, over, the, lazy, dog');
$smarty->assign('home_content' , 'this is the home page content that gets inserted into');
$smarty->display('template.tpl');
?>
And that is just scratching the surface of what a templating system can do. you can repeating or optional bocks, include other templates, etc etc.

Categories