Dear All,
in Boston PHP Meetup currently we are currently using this book PHP and MYSQL6 edition which Kevin Yank and TOM Butler co-author and I am the assistant organizer.
1). I want to ask, how w can be talking to the authors if we have any area that is not clear to us?
2) I am having problem with this code $route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');
on page 376 of the e-book. I am using Xampp which uses apache server and my index.php is not inside the root directory is in
http://localhost/phpmysqlnoviceninja6th/Chapter09/public/index.php
.
sir, instead that this code ($_SERVER['REQUEST_URI'], '?')
should just add index.php to the end of my url, it always add the who url to the initial one like this
http://localhost/phpmysqlnoviceninja6th/Chapter09/public/phpmysqlnoviceninja6th/chapter09/public/
.
please, what do I do for code to just add the index.php to the url?
I need the solution ASAP to continue enjoying the book.
I donât know, but if you have PHP-related queries you can post them on here and people will try to help, whether theyâre authors or not. Plenty of people will have read the book and maybe encountered the same issues. But post the code youâre asking for help with, not just a page number, so that people who donât have the book to hand, or donât have the same edition, may still help.
I have added book and support tags toy your post.
@TomB is sometimes active in the forums to offer help.
Hi olaoyesunday,
I am having problem with this code $route = ltrim(strtok($_SERVER[âREQUEST_URIâ], â?â), â/â); on page 376 of the e-book. I am using Xampp which uses apache server and my index.php is not inside the root directory is in
The line of code you quoted takes the URL e.g. https://192.168.10.10/jokes/edit?id=4
and extracts the parts between the domain name and the question mark (if there is one, otherwise the end of the URL.
The URL i mentioned would set the $route
variable to /jokes/edit
which is then used for routing to a controller action.
To use index.php
in a different subdirectory such as yours, the $route
variable will contain the string /phpmysqlnoviceninja6th/Chapter09/public/joke/list
.
Youâll need to remove /phpmysqlnoviceninja6th/Chapter09/public
from the string. The simplest way is to add the line
$route = str_replace('/phpmysqlnoviceninja6th/Chapter09/public', '', $route)
There is likely a better way of working out what the extra part of the URL is from various $_SERVER
variables. However, I recommend against hosting websites like this from a single URL. Youâre better off setting up virtual hosts on your apache installation so that you can have , for example http://phpnoviceninja.localhost/
.
Thanks Sam47 for linking me with TomB.
Dear TomB,
thanks for your prompt reply and explanation. I tried using string replacement but it still not work. do I need to do anything to my apache server. I checked my apache serve and it supported mod rewriting I even installed wordpress on it check it htaccess file it shows that it supported mod rewriting. I typed $route = str_replace(â/phpmysqlnoviceninja6th/Chapter09/publicâ,ââ, $route); after $route = ltrim(strtok($_SERVER[âREQUEST_URIâ], â?â), â/â);
then entering this url âlocalhost/phpmysqlnoviceninja6th/Chapter09/public/â it is still showing âhttp://localhost/phpmysqlnoviceninja6th/Chapter09/public/phpmysqlnoviceninja6th/chapter09/public/â instead of it adding an index.php to it. is there anything I have to do to my apache configurations?
Youâll need to create the relevant .htaccess file with rewrite rules. The rules for this file are included on the book (apologies, Iâm at work at the moment and donât have the book to hand, but itâs mentioned on the page that talks about url rewriting)
Thanks TomB For your reply. Initially I did not want to bother you again with this issue but as I have said in my first mail that we are currently using your book at Boston PHP meetup in PHP PRECOLATE SEASON 10 https://www.meetup.com/bostonphp/messages/boards/thread/51274527
. we supposed to be at Chapter 13 by now and majority of my people have stop responding to the assignments due to this problem because majority of us are using xampp on our Laptops. .htaccess you wrote for apache is this
conf
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L,QSA]
it prevents page from displaying, it normally shows Server error! please, what can we adjust in this .htaccess file?
The code is correct, youâd have to examine the server logs to see what the cause of the issue is. Iâm afraid Iâm not sure how youâd go about that using xampp, I havenât run a web server on windows for years. Is mod_rewrite enabled in httpd.conf?
Iâd recommend using vagrant/virtualbox as a development environment anyway ( see https://www.sitepoint.com/re-introducing-vagrant-right-way-start-php/). If people canât boot the VM you probably just need to enable VT-x in the computerâs BIOS, it can be named differently depending on the make of the PC. âVirtualization technologyâ âIntel Virtualizationâ, etc but it all means the same thing.
I have the same problems described above with my students. Basically we stuck on p.375 - 376 using xampp on Ubuntu v.14+ and on Windows 10. The file .htaccess with the content above causes that the directory which .htaccess is in disappears from the view by browser. The full URL to that directory in the browser shows Server error on windows and Ubuntu. Tom, do you have any idea how to make chapter 9 and 10 working.
JG
Just a note. In windows I think you canât create directly a file starting with dot (.htaccess) but you can change the .htaccess file name read by apache in apache config with the following directive:
AccessFileName htaccess.txt
And reboot apache.
Sure you can. Windows doesnât care how you name your files, if you want to it to start with a dot thatâs fine.
I would recommend against this, as that can be viewed by anyone visiting your site, whereas access to files that start with a dot are rejected by default in Apache.
Yes, I wouldnât recommend that on live site either. I used that on local win machine and .htaccess on live site (rhel).
This is what I get when I try to create a file starting with dot in my win10 instance:
You canât in Windows Explorer. If you try, you get an error âYou must type a file nameâ.
On Ubuntu can you paste the output of:
tail -n 200 /var/log/apache2/error.log
Make sure the .htaccess
file contains only the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L,QSA]
Things may have changed, but last I knew it could be done by enabling âshow hidden filesâ and opening the text editor âas Adminâ. And there have been times Iâve needed to stop the server to not get a âin useâ error.
Hi, Tom.
(I have had to rework this post as it contained virtual host settings which, while only generic, the forum took to be âlinksâ and therefore would not let me post⌠so this is the non-link version⌠Basically, I have removed the two periods/full stops from wwwframeworklocal that should be either side of the word framework. Hope this still makes sense.)
I hope youâre still around in the forum. Iâm really pleased I found it as, like others here, Iâm banging my head against a brick wall (well⌠a stud partition, but it still hurts ).
Before I moan, can I say that the concept of your book: to end up with a reusable PHP framework, is outstanding. Hence my perseverance to get the code working. Well done!
Like olaoyesunday, I am failing to arrive at the correct text for the routes after coding up to page 378 of the book.
I have set up the following:
- a â.htaccessâ file containing:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L,QSA]
- a virtual host, wwwframeworklocal, containig the following:
<VirtualHost *:80>
ServerAdmin admin@framework.local
ServerName wwwframeworklocal
DocumentRoot /var/www/wwwframeworklocal
ErrorLog ${APACHE_LOG_DIR}/wwwframeworklocal_error.log
CustomLog ${APACHE_LOG_DIR}/wwwframeworklocal_access.log combined
</VirtualHost>
I am using Debian and the default apache document root is /var/www/html and virtual hosts must ref /var/www/.
- an /etc/hosts file containing:
127.0.0.1 wwwframeworkcom
- I have enabled mod_rewrite module in Apache and restarted the server.
In my IDE (Netbeans), I have a folder, âwwwframeworklocalâ as the website root. In here, I have folders for classes, includes and templates, and I have the index.php and jokes.css and .htaccess files. So, a slight difference to you in that the index.php, jokes.css and .htaccess files are not in the âpublicâ directory. I prefer to have these in the root if the website. I have, however, altered the few links to things like templates/* from /âŚ/templates⌠to /templates⌠and these links work well.
The whole site worked beautifully just before I did the URL Rewrite changes, i.e. when I was using $route = $_GET[ârouteâ] ?? âjoke/homeâ;
So I know there are no problems with the fact that I havenât used a âpublicâ directory: all links are working fine.
OK. So thatâs where I am. Hereâs what Iâm getting:
Forget the IDE for now⌠just using Chrome web browser, if I enter as my URL the following: âwww.framework.localâ, eureka, I get the website, success! So, my /etc/hosts is good, the virtual host is good, the .htaccess is good.
If I hover over the three links, I get the following as target urls:
Home: wwwframeworklocal
Joke List: wwwframeworklocal/joke/listall
Add a new joke: wwwframeworklocal/joke/edit
(note: the IDE wouldnât let me use âlistâ as the name of the function in JokeController.php. I had to change this to something else. I chose âlistallâ and ensured I changed that everywhere in the code. Again, this worked fine when the code was at the $route = $_GET[ârouteâ] ?? âjoke/homeâ; stage so nothing wrong with this.
Right. You say the new url rewriting â$routeâŚâ line in index.php should leave you with:
the full URL starting after the domain name and ending either before the first question mark or at the end of the whole url if there is no question mark.
Now, whether or not we have used a âpublicâ directory, we all seem to have placed all of the code inside a folder of some kind/name, phpmysqlnoviceninja6th/Chapter09 from olaoyesunday above or wwwframeworklocal for me. We do this so we can develop more than one website in our IDEs. Itâs no good placing the code in the apache root as then, what do we do with a second website? Put that in the apache root as well and mix all the websitesâ files together? No. Each website is in a folder of its own inside the Apache root. Hence the virtual host. In your book, after youâve told us about the .htaccess file, you tell us to go to 192.168.10.10/joke/list and the new $route line will leave us with a value of âjoke/listâ. I get that. But what value is the IP address. My Apache, like most I would imagine is running at 127.0.0.1. If I just use this in my URL, I end up looking in the default doc root: /var/www/html. I donât end up in a virtual host location. I dont end up looking at the index.php file which you have told us to put in the âpublicâ directory. If we set up our website, even in the default doc root, you have asked us to put an index.php file in âpublicâ. So doing what you ask⌠192.168.10.10/joke/list⌠isnât going to work.
So⌠against all common sense (I think), I deduce from your IP approach that I need all my files in the default doc root with no âpublicâ folder so that â127.0.0.1â with no filename will default to index.php. Great this works. I get the site as expected. Looking at the 3 navigation links, hovering, I get:
Home: 127.0.0.1
List: 127.0.0.1/joke/listall
Edit: 127.0.0.1/joke/edit
Clicking home refreshes the page and the site is still there. Clicking either of the other two results in:
Not Found
The requested URL /joke/listall was not found on this server.
Apache/2.4.25 (Debian) Server at 127.0.0.1 Port 80
So ⌠???
Please Tom, I paid £25.00 for the book. I have invested a little over a month of my life getting to page 378. I am very much looking forward to realising the concept of the book: to end up with a generic framework. Then⌠my stud partition needs repair.
I think (I could be wrongâŚ) what is happening here is that, if we follow the book to the letter and install the whole Vagrant/VitualBox/Homestead thing, the code will probably work because of environment settings in the VM. But we donât all want to do that. I have some experience in working with servers and I can install and configure apache, php, mariadb well. I want to just use the default Debian set up. Part of the reason for this is that my web host uses Debian and itâs default LAMP set up so I want my development code to work in production.
I thought I was buying and investing time in a book called âPHP & Mysql: Novice to Ninjaâ. I didnât think I was buying a book called âPHP & Mysql: Novice to Ninja, if you code in Vagrant/VirtualBox/Homestead or it wonât workâ.
Tom, please may I respectfully request you do this:
$title = âPHP & Mysql: Novice to Ninja, if you code in Vagrant/VirtualBox/Homestead or it wonât workâ
$title = str_replace(âif you code in Vagrant/VirtualBox/Homestead or it wonât workâ, ââ, $title)
During the book, youâre constantly making code generic. Marvellous. But if none of it works outside of the single environment you have used, itâs not generic at all is it.
There are reviews on the SitePoint website for your book saying the code doesnât work and the book is full of typos. Thereâs too much truth in those reviews. There are course deliverers on this forum whose students are leaving their courses.
I donât think this is good.
Help us out Tom, quickly if you can. I am very happy to help you with any questions you have about a generic set up such as mine.
You should be able to get around that by formatting code and URLs as code.
For in-line code
put one back-tick at the beginning and end.
For blocks of code put three back-ticks on a line of their own before and after the block.
Or highlight and click the </>
button on the editor toolbar.
Sam,
Cheers. Thatâs helpful. Iâll remember that for next time.
Mike
Hi Mike,
If you are using a virtualhost, youâll need to use that everywhere. You donât need to set up a virtualhost to use URL rewriting but if you have youâll need to us that consistently. So if your website is on http://www.framework.local/
youâll need to visit e.g. http://www.framework.local/joke/edit
and not http://127.0.0.1
.
Ignoring every single line of PHP code from the book you should be able to create this .htacces
file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L,QSA]
And this index.php
:
<?php
echo 'You are viewing ' . $_SERVER['REQUEST_URI'];
And visit http://localhost/foo/bar
(or whatever your URLis/foo/bar
)and see the message âYou are viewing /foo/barâ any URL you visit on the website should load index.php and display the path on the server you are viewing.
If that isnât working, please paste the output of the severâs error log as mentioned above.
Every line of code works on PHP 7+. As you have pointed out there are some pieces of code which wonât work on PHP 5.6 because itâs outdated. URL rewriting is a different topic than PHP as itâs server specific. As I mentioned in the installation chapter, the simplest way to get up and running is using the Homestead Improved vagrant box: Everything is configured for you so will just work. If you choose to use a different web server, you will need to do some configuration yourself and have an understanding of server configuration which is beyond the scope of the book.