PHP - URL Issue - php

i can't figure out what is the issue. Here is the pictures
https://www.website.com/index.php
I works good as you can see below.
But once if i type /index.php/index.php or /index.php/{any characters}. For example
https://www.website.com/index.php/popooopii2323**
How can i fix this, this is not only issue with index.php, even if login and goto dashboard or any other page within the UCP, i face this issue. Please help me to fix this issue.
Thanks you!

[Note: It would have been better if you could share some code segment from index.php file. I am just assuming your problem. It may or may not solve your issue. Anyway try this.]
The problem I'm guessing is with your path to .css file. Try to change you .css file path as following in your index.php file:
(Assuming, your path to .css file is {project_root}/public/path/to/style.css)
<link type="text/css" rel="stylesheet" href="https://www.website.com/path/to/style.css">
[P.S.]: Also, change your background image path as https://www.website.com/path/to/bg_image.png

try to first put css file in head part of html. Please notice the link starting with abosolute path /.
<link rel="stylesheet" href="/css/style.css">
In that file, set background with relative path
.myBackground { background: url('../img/bg.jpg'); }
Then put style.css into folder css. And image into folder img.

Related

How to make Dynamic css path?

I'm using not_found.php file for unfound files on server , and it's in Main Directory
it's style is in 'assests/css/main.css' from same Directory ,
so I make href of linking stylesheet
href="assests/css/main.css"
it works perfect if user types 'Main/wrongFile' for example , but if user typed 'Main/ez/wrongFile' , stylesheet is included wrongly due to changing of path
I'm searching for function like
$Path = FindPath('Main/assests/css/main.css');
so it gets that path wherever file is in
then I can simply
href="<?php echo $Path; ?>"
The best way to fix it is using the code below;
<link rel="stylesheet" href="/assests/css/main.css" type="text/css"/>
adding / at beginning of assests/css/main.css simply tells browsers which root directory to look at.
Option 2) Using htaccess. Add the code below into your .htaccess file
RewriteRule ^([^/]*)/assests/css/main.css$ assests/css/main.css [R=301,L,NC]

Why won't this PHP code feed in to the link TAG href attribute? (local host, not using laravel)

Disclaimer: I haven't got a clue what I'm doing with PHP I'm just playing around with it.
I have my css file in a folder named CSS and then my header.php and footer.php in the main site folder. If i include the header.php in other directories I am just using:
<?php include('../header.php'); ?>
I know this isn't the way to do it however I don't know how to configure it probably (with a config.php file etc..) but my issue is, once the header's included in files in any directory of course it will look for the css/main.css file in that folder so I've tried doing the following:
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']. '/JAGS/css/main.css' ?>" />
When I use the php line in the body it displays the path
E:/xampp/htdocs/JAGS/css/main.css
but if I use it there in the link tag then doesn't work.
What seems to be my problem other than the fact I'm clueless with PHP. Is there something else I should be using? Is there something I need to do in my xampp config files?
Edit: By "doesn't work" I mean the styles are not being applied.
Edit 2: Inspecting shows the following:
<link rel="stylesheet" href="E:/xamppp/htdocs/JAGS/css/main.css">
I know there's an extra p on the end of xampp, this is actually what I have the folder named. Is it because it's not saying "localhost/JAGS/CSS/main.css"? If so what would be the reason for this?
Edit 3: Console shows error below:
Not allowed to load local resource: file:///E:/xamppp/htdocs/JAGS/css/main.css
Edit 4: Not using Laravel
Thank you
Why do you require the document root, you should just place a dot in front of it and set the base url in a meta tag.
<base href="yourdomain.com">
<link rel="stylesheet" href="./JAGS/css/main.css'/>

Ways to resources in PHP

I try to resolve my problem with resource path in Php. In file header.php I include scripts and stylesheets, but when I require_once('../templates/header.php'); in file login.php from views directory I get 404 error code on all my resources, because all files must be in subdirectory of views direcroty. How I can solve this problem?
Path:
/var/www/reg/templates/header.php - path to header
/var/www/reg/views/login.php - path to login
/var/www/reg/js/script.js - path to js
/var/www/reg/css/style.css - path to css
according to your file structure
inside header.php
try this way:
<link href="/reg/css/style.css" rel="stylesheet" />
<script src="/reg/js/script.js" ></script>
As I have tested it works for me.
This is how I'd probably do it, not sure if best practice, but similar to the way wordpress works, IE
In your a index file (in the reg/ dir or in a config file if you have one):
define('SITEPATH', dirname(__FILE__).'/');
Then you can require files like this:
require_once(SITEPATH . 'templates/header.php');
Then when you're calling your resource files, you can do something similar this:
<script src="<?php echo SITEPATH; ?>js/script.js"></script>
But, imo, backend stuff shouldn't really be on template files, but because your directories are different to how I'd lay them out.

Include PHP file's JS, Css

I am trying to include a PHP file in another directory.
Here is my file structure
settings\
manage.php
website \
index.php
main.js
main.css
For manage.php
echo 'Welcome to the manager! Here is your website:';
include('../website/index.php');
index.php
<script src='main.js'></script>
<link rel="stylesheet" type="text/css" href="main.css">
So, when I load manage.php, I do not get main.js or main.css. How can I get this to work? Maybe include is not the right way to go? I cannot modify anything in the website folder.
[I know iFraming is a possible solution but I'm hoping to get another answer]
Since you cannot edit /website/ content you should could try this ugly code for a startup.
Add following just before include statement in your manage.php
echo('<base href="../website/">');
If it works for you, then you can think of sending a correct header with PHP before including a html file, instead of directly echoing a base tag.
Please consider comments of jeroen as down-to-earth solution and use frames
The problem here is that when the browser loads /settings/manage.php it will make requests for /settings/main.js and /settings/main.css which don't exist
You probably need to change your html in index.php to something like this:
<script src="/website/main.js"></script>
<link rel="stylesheet" type="text/css" href="/website/main.css">
Note I've made some assumptions about your URLs based on your directory layout so you may need to adjust my solution to make it work for you
Based on the comment below the question: If you want to include some functions / code for your users (instead of the other way around; you including user's stuff in your code), you should look into the auto_prepend_file directive.
Basically, you specify in your php.ini file that you want to prepend (as a require) a certain php file before the main file.
Edit: As you don't have access to php.ini but you can use a .htaccess file, you can put this in your .htaccess:
php_value auto_prepend_file "/path/to/your/file.php"

Missing CSS file and images after URL rewrite

I'm trying to make user friendly URL using mode rewrite.
My problem is, that after giving category like 'name' to my URL, when I call the page using new URL, it can't load the CSS file or images.
I have a link like:
localhost/mywebsite/project?id=22
New link is something like
localhost/mywebsite/project/22/myproject.project
htaccess code:
RewriteRule ^project/([0-9]*)/.*\.project$ /project.php?project=$1 [L]
(it might not be 100% right but I don't have access to my code right now so I just wrote this and it works fine on the original source)
My root directory is localhost/mywebsite/
and my CSS file is in css/style.css
localhost/mywebsite/css/style.css
my htaccess
localhost/mywebsite/.htaccess
and my project.php file is in
localhost/mywebsite/project.php
So in the project page I have access to CSS file by using relative path,
<link href="css/style.css" rel="stylesheet" type="text/css" />
but when I use rewritten URL page can't find the CSS file.
I can't use absolute path with domain name because I don't have domain yet! and it can be anything.
one way is to use relative path to domain as suggested on the similar questions
localhost/mywebsite/project.php
and when i run my script localy my root directory is
localhost
so css link should look like
href="mywebsite/css/style.css"
but when i go live i should change all links to probably something like
href="/css/style.css"
this seems like lots of work
For your local version add
<base href="//localhost/mywebsite" />
to the head section
and for your live versions change it to
<base href="//your.domain.here" />
reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4
you have to define the base path or the server view path in the connection.php and whenever u want that path, make that global. then that variable will b called and the css or images will take the whole path.
for example
$SVP="http://www.example.com/"
global $SVP;
echo $SVP;
so
Insert an image into the same file with the same relative path as the css href link, load the page in a browser, right-click the image in internet explorer, click properties and you should see where the relative path actually points to.

Categories