php $_SESSION on server undefined on local angular application - php

I am working on a project which require sessions. I have my PHP files on a server and I am coding the front end in Angular, locally at the moment.
I am creating a $_SESSION in my php file for when a user logs in. Creating the post request on my local angular project, any $_SESSION property is returned as empty (e.g. $_SESSION['key'] is empty). However, it works fine when testing on Postman, and also works fine when I compile my angular app and put it on the server... So it works on Postman, and on the angular app on the server, but not the angular app locally (with the exact same code).
I have added CORS to my PHP files, and can use many other post/get requests locally, just not the session.
Is there any particular reason this doesn't work, or a way to get it working locally? As I don't want to compile and upload the project every time to test it out.
Any other information needed, please let me know.
Thank you in advance.

Related

Creating an Angular project that uses a PHP login system running on Xampp

I have created a login system running on XAMPP for a web app using php, html, materialise for css, and a tiny bit of javascript. The system consists of login, signup, and index pages. In addition, a database is implemented to store user data and app data in the future. If all form validation is verified, a user's sign up details are stored in the database, after which they can log in normally. If a user successfully logs in, a session is started using the start_session() and SESSION global variable of php. The index page indicates whether a session is currently in progress.
Once a session is in progress, I want to display the application contents (yet to be developed) on the index page where a user can practice their functionalities and log out when they want. I want to implement this logic using angular but I'm confused as to how I'm going to get angular to work with the php login system and the database that are running on xampp. I'm a beginner to angular (and development in general) but from what I understand it comes with it's own "server" so the application contents can be hosted locally. Does anyone have any idea how I can get the angular app working with my implementated database and php login system? Thanks in advance!
I am learning angular now a day but I think I can answer your question. By default angular serve your application on localhost:4200. This application would contain your UI and interaction logic. After the app gets loaded in the browser, your application would call PHP script as you would have normally called with HTML because now everything is on browser so you need not to bother about angular server.
However in angular, you should utilize angular services to make server request.
Your web architecture would look like this (think of apache in place of nginx)

Problems with using session data using nw.js with PHP web server

I am trying to convert my web game into a desktop app. Everything works except for the session data. I got websockets, images, audio, and all of that working. Great! But when I try to authenticate, it POSTs to the web server just fine, returns a successful authentication, and attempts to reload the page. However, upon page reload, the subsequent AJAX requests cannot get any of the $_SESSION values again.
I was able to find my PHPSESSID using win.cookies.getAll inside of the nw.js app, but I don't see how this helps me at all. Normally this is automatic when loading the webpage from the server. How do I fix this?

Create React App, data via AJAX, PHP page backend on XAMPP Apache

I'm trying to learn React by replicating a simple CRUD page I created earlier.
The old CRUD page was a Javascript/jQuery front-end with a form. It sent data via AJAX to a PHP backend page, and the PHP page interacted with a mySQL/mariaDB database.
Everything was locally hosted on my PC on XAMPP Apache, and everything worked fine.
I have now managed to replicate the front end using React, via Create React App and it's running on localhost:3000
I am now stuck. I don't know how to make my front-end (on localhost:3000) send data via AJAX to my back-end PHP page (on localhost:80 running on XAMPP Apache)
There are a lot of us PHP / mariaDB types out there who want to continue using that sub-stack as our back-end. The answers to this question will be enormously helpful in encouraging our crowd to try Create React App and React (I'm loving React btw!). (I was surprised that when I searched [create-react-app] [php] in StackOverflow, I got only 2 hits.)
Update: if this is off-topic for SO, then I'd appreciate suggestions on which SE I should post this on... thanks!
Answering my own question.
The basic problem is one of CORS (cross-origin resource sharing). The error one gets is (in the Chrome console):
No 'Access-Control-Allow-Origin' header is present on the requested resource.
During development only, this issue is easily resolved by adding this line of code to the top of the PHP file:
<?php
header("Access-Control-Allow-Origin: *");
This code should never be left there in production unless you're okay with anyone in the world accessing the PHP file API.
In actual production of this React app, one would expect that the JavaScript and the PHP files would share the same domain, so there won't be a CORS issue.

Android Php Server Connection

I am trying to make an android app with php in a backend. In this login page I am trying to send login credentials to php file so I can echo that and store that value in variables. And also My php file is stored on server of www.myurl.com/myfolder/login.php. I know a lot more about web development but this is my first experiment with android app. I even referred several online tutorials for this. All I see is using JSON and json_encode in php but that is not solving my problem. I think I am making some other mistake or is not able to locate an error.

How to run php file in phonegap?

I am developing a mobile application in phonegap. My app works in a way that when a user submits a form, the data gets posted to a file called functions.php (present in the root folder) and functions.php parses data and sends to my rest application using curl. When I run this application in a web server, it works perfectly fine but when i run it in phonegap and post data to functions.php, instead of running functions.php as a function, it displays all the code of functions.php on the mobile screen and stops there. Does anyone know whats going on?
The situation you describe is what happens when you try to run a php file from a webserver that does not have php installed, so it sounds like you are trying to load the functions.php locally from within your phonegap app. Phonegap has its own webserver, which is not a php server, so it won't run php files. If you need to interact with php/sql/other remote stuff, you should be using ajax requests to interact with the remote (non-phonegap) server. https://forum.jquery.com/topic/how-do-i-get-ajax-working-with-phonegap

Categories